clear
set more off

**************************************************
*Title: H_SHARE_long
*Summary: converts data from the SHARE to create the Harmonized SHARE ver D
*Author: Sidney Beaumaster, Sandy Chien, Samuel Lau, Ashley Lin, Drystan Phillips, Jenny Wilkens, and Jinkook Lee
*Date Created: September 2017
**************************************************

***define folder locations***
local overwaves "$path/data/sharewX_rel6-0-0_gv_longitudinal_weights_stata"
local inputw1 "$path/data/sharew1_rel6-0-0_ALL_datasets_stata"
local inputw2 "$path/data/sharew2_rel6-0-0_ALL_datasets_stata"
local inputw3 "$path/data/sharew3_rel6-0-0_ALL_datasets_stata"
local inputw4 "$path/data/sharew4_rel6-0-0_ALL_datasets_stata"
local inputw5 "$path/data/sharew5_rel6-0-0_ALL_datasets_stata"
local inputw6 "$path/data/sharew6_rel6-0-0_ALL_datasets_stata"
local output "$path/data"

***define files***
*All Wave files
global tracker "`overwaves'/sharewX_rel6-0-0_gv_allwaves_cv_r"
global long_weight_w1w2 "`overwaves'/sharewX_rel6-0-0_gv_longitudinal_weights_w1w2"
global long_weight_w1w6 "`overwaves'/sharewX_rel6-0-0_gv_longitudinal_weights_w1-w6"
global long_weight_w2w3 "`overwaves'/sharewX_rel6-0-0_gv_longitudinal_weights_w2w3"
global long_weight_w3w4 "`overwaves'/sharewX_rel6-0-0_gv_longitudinal_weights_w3w4"
global long_weight_w4w5 "`overwaves'/sharewX_rel6-0-0_gv_longitudinal_weights_w4w5"
global long_weight_w5w6 "`overwaves'/sharewX_rel6-0-0_gv_longitudinal_weights_w5w6"

*Wave 1 Capi Modules files
global wave_1_ac "`inputw1'/sharew1_rel6-0-0_ac"
global wave_1_as "`inputw1'/sharew1_rel6-0-0_as"
global wave_1_br "`inputw1'/sharew1_rel6-0-0_br"
global wave_1_cf "`inputw1'/sharew1_rel6-0-0_cf"
global wave_1_ch "`inputw1'/sharew1_rel6-0-0_ch"
global wave_1_co "`inputw1'/sharew1_rel6-0-0_co"
global wave_1_cv_h "`inputw1'/sharew1_rel6-0-0_cv_h"
global wave_1_cv_r "`inputw1'/sharew1_rel6-0-0_cv_r"
global wave_1_dn "`inputw1'/sharew1_rel6-0-0_dn"
global wave_1_ep "`inputw1'/sharew1_rel6-0-0_ep"
global wave_1_ex "`inputw1'/sharew1_rel6-0-0_ex"
global wave_1_ft "`inputw1'/sharew1_rel6-0-0_ft"
global wave_1_gs "`inputw1'/sharew1_rel6-0-0_gs"
global wave_1_hc "`inputw1'/sharew1_rel6-0-0_hc"
global wave_1_hh "`inputw1'/sharew1_rel6-0-0_hh"
global wave_1_ho "`inputw1'/sharew1_rel6-0-0_ho"
global wave_1_iv "`inputw1'/sharew1_rel6-0-0_iv"
global wave_1_mh "`inputw1'/sharew1_rel6-0-0_mh"
global wave_1_ph "`inputw1'/sharew1_rel6-0-0_ph"
global wave_1_sp "`inputw1'/sharew1_rel6-0-0_sp"
global wave_1_ws "`inputw1'/sharew1_rel6-0-0_ws"

*Wave 1 Generated Variables files
global wave_1_gv_dol "`inputw1'/sharew1_rel6-0-0_gv_dol"
global wave_1_gv_health "`inputw1'/sharew1_rel6-0-0_gv_health"
global wave_1_gv_housing "`inputw1'/sharew1_rel6-0-0_gv_housing"
global wave_1_gv_isced "`inputw1'/sharew1_rel6-0-0_gv_isced"
global wave_1_gv_isco "`inputw1'/sharew1_rel6-0-0_gv_isco"
global wave_1_gv_support "`inputw1'/sharew1_rel6-0-0_gv_support"
global wave_1_gv_weights "`inputw1'/sharew1_rel6-0-0_gv_weights"
global wave_1_imputations "`inputw1'/sharew1_rel6-0-0_gv_imputations"

*Wave 1 Dropoff and Vignette files
global wave_1_dropoff "`inputw1'/sharew1_rel6-0-0_dropoff"
global wave_1_vignettes "`inputw1'/sharew1_rel6-0-0_vignettes"

*Wave 1 Technical Variables files
global wave_1_techvar "`inputw1'/sharew1_rel6-0-0_technical_variables"

*Wave 2 Capi Modules files
global wave_2_ac "`inputw2'/sharew2_rel6-0-0_ac"
global wave_2_as "`inputw2'/sharew2_rel6-0-0_as"
global wave_2_br "`inputw2'/sharew2_rel6-0-0_br"
global wave_2_cf "`inputw2'/sharew2_rel6-0-0_cf"
global wave_2_ch "`inputw2'/sharew2_rel6-0-0_ch"
global wave_2_co "`inputw2'/sharew2_rel6-0-0_co"
global wave_2_cs "`inputw2'/sharew2_rel6-0-0_cs"
global wave_2_cv_r "`inputw2'/sharew2_rel6-0-0_cv_r"
global wave_2_dn "`inputw2'/sharew2_rel6-0-0_dn"
global wave_2_ep "`inputw2'/sharew2_rel6-0-0_ep"
global wave_2_ex "`inputw2'/sharew2_rel6-0-0_ex"
global wave_2_ft "`inputw2'/sharew2_rel6-0-0_ft"
global wave_2_gs "`inputw2'/sharew2_rel6-0-0_gs"
global wave_2_hc "`inputw2'/sharew2_rel6-0-0_hc"
global wave_2_hh "`inputw2'/sharew2_rel6-0-0_hh"
global wave_2_ho "`inputw2'/sharew2_rel6-0-0_ho"
global wave_2_iv "`inputw2'/sharew2_rel6-0-0_iv"
global wave_2_mh "`inputw2'/sharew2_rel6-0-0_mh"
global wave_2_pf "`inputw2'/sharew2_rel6-0-0_pf"
global wave_2_ph "`inputw2'/sharew2_rel6-0-0_ph"
global wave_2_sp "`inputw2'/sharew2_rel6-0-0_sp"
global wave_2_ws "`inputw2'/sharew2_rel6-0-0_ws"
global wave_2_xt "`inputw2'/sharew2_rel6-0-0_xt"

*Wave 2 Generated Variables files
global wave_2_gv_health "`inputw2'/sharew2_rel6-0-0_gv_health"
global wave_2_gv_housing "`inputw2'/sharew2_rel6-0-0_gv_housing"
global wave_2_gv_isced "`inputw2'/sharew2_rel6-0-0_gv_isced"
global wave_2_gv_weights "`inputw2'/sharew2_rel6-0-0_gv_weights"
global wave_2_imputations "`inputw2'/sharew2_rel6-0-0_gv_imputations"

*Wave 2 Dropoff files
global wave_2_dropoff "`inputw2'/sharew2_rel6-0-0_dropoff"

*Wave 2 Vignettes files
global wave_2_vignettes "`inputw2'/sharew2_rel6-0-0_vignettes"

*Wave 2 Technical Variables files
global wave_2_techvar "`inputw2'/sharew2_rel6-0-0_technical_variables"

*Wave 3 Capi Modules files
global wave_3_ac "`inputw3'/sharew3_rel6-0-0_ac"
global wave_3_cs "`inputw3'/sharew3_rel6-0-0_cs"
global wave_3_cv_r "`inputw3'/sharew3_rel6-0-0_cv_r"
global wave_3_dq "`inputw3'/sharew3_rel6-0-0_dq"
global wave_3_fs "`inputw3'/sharew3_rel6-0-0_fs"
global wave_3_gl "`inputw3'/sharew3_rel6-0-0_gl"
global wave_3_gs "`inputw3'/sharew3_rel6-0-0_gs"
global wave_3_hc "`inputw3'/sharew3_rel6-0-0_hc"
global wave_3_hs "`inputw3'/sharew3_rel6-0-0_hs"
global wave_3_iv "`inputw3'/sharew3_rel6-0-0_iv"
global wave_3_rc "`inputw3'/sharew3_rel6-0-0_rc"
global wave_3_re "`inputw3'/sharew3_rel6-0-0_re"
global wave_3_rp "`inputw3'/sharew3_rel6-0-0_rp"
global wave_3_st "`inputw3'/sharew3_rel6-0-0_st"
global wave_3_wq "`inputw3'/sharew3_rel6-0-0_wq"
global wave_3_xt "`inputw3'/sharew3_rel6-0-0_xt"

*Wave 3 Generated Variables files
global wave_3_gv_weights "`inputw3'/sharew3_rel6-0-0_gv_weights"

*Wave 4 Capi Modules files
global wave_4_ac "`inputw4'/sharew4_rel6-0-0_ac"
global wave_4_as "`inputw4'/sharew4_rel6-0-0_as"
global wave_4_br "`inputw4'/sharew4_rel6-0-0_br"
global wave_4_cf "`inputw4'/sharew4_rel6-0-0_cf"
global wave_4_ch "`inputw4'/sharew4_rel6-0-0_ch"
global wave_4_co "`inputw4'/sharew4_rel6-0-0_co"
global wave_4_cv_r "`inputw4'/sharew4_rel6-0-0_cv_r"
global wave_4_dn "`inputw4'/sharew4_rel6-0-0_dn"
global wave_4_ep "`inputw4'/sharew4_rel6-0-0_ep"
global wave_4_ex "`inputw4'/sharew4_rel6-0-0_ex"
global wave_4_ft "`inputw4'/sharew4_rel6-0-0_ft"
global wave_4_gs "`inputw4'/sharew4_rel6-0-0_gs"
global wave_4_hc "`inputw4'/sharew4_rel6-0-0_hc"
global wave_4_hh "`inputw4'/sharew4_rel6-0-0_hh"
global wave_4_ho "`inputw4'/sharew4_rel6-0-0_ho"
global wave_4_iv "`inputw4'/sharew4_rel6-0-0_iv"
global wave_4_mh "`inputw4'/sharew4_rel6-0-0_mh"
global wave_4_pf "`inputw4'/sharew4_rel6-0-0_pf"
global wave_4_ph "`inputw4'/sharew4_rel6-0-0_ph"
global wave_4_sn "`inputw4'/sharew4_rel6-0-0_sn"
global wave_4_sp "`inputw4'/sharew4_rel6-0-0_sp"
global wave_4_xt "`inputw4'/sharew4_rel6-0-0_xt"

*Wave 4 Generated Variables files
global wave_4_gv_health "`inputw4'/sharew4_rel6-0-0_gv_health"
global wave_4_gv_housing "`inputw4'/sharew4_rel6-0-0_gv_housing"
global wave_4_gv_isced "`inputw4'/sharew4_rel6-0-0_gv_isced"
global wave_4_gv_network "`inputw4'/sharew4_rel6-0-0_gv_networks"
global wave_4_gv_weights "`inputw4'/sharew4_rel6-0-0_gv_weights"
global wave_4_imputations "`inputw4'/sharew4_rel6-0-0_gv_imputations"

*Wave 4 Dropoff files
global wave_4_dropoff "`inputw4'/sharew4_rel6-0-0_dropoff"

*Wave 4 Technical Variables files
global wave_4_techvar "`inputw4'/sharew4_rel6-0-0_technical_variables"

*Wave 5 Capi Modules files
global wave_5_ac "`inputw5'/sharew5_rel6-0-0_ac"
global wave_5_as "`inputw5'/sharew5_rel6-0-0_as"
global wave_5_br "`inputw5'/sharew5_rel6-0-0_br"
global wave_5_cf "`inputw5'/sharew5_rel6-0-0_cf"
global wave_5_ch "`inputw5'/sharew5_rel6-0-0_ch"
global wave_5_co "`inputw5'/sharew5_rel6-0-0_co"
global wave_5_cs "`inputw5'/sharew5_rel6-0-0_cs"
global wave_5_cv_r "`inputw5'/sharew5_rel6-0-0_cv_r"
global wave_5_dn "`inputw5'/sharew5_rel6-0-0_dn"
global wave_5_ep "`inputw5'/sharew5_rel6-0-0_ep"
global wave_5_ex "`inputw5'/sharew5_rel6-0-0_ex"
global wave_5_ft "`inputw5'/sharew5_rel6-0-0_ft"
global wave_5_gs "`inputw5'/sharew5_rel6-0-0_gs"
global wave_5_hc "`inputw5'/sharew5_rel6-0-0_hc"
global wave_5_hh "`inputw5'/sharew5_rel6-0-0_hh"
global wave_5_ho "`inputw5'/sharew5_rel6-0-0_ho"
global wave_5_it "`inputw5'/sharew5_rel6-0-0_it"
global wave_5_iv "`inputw5'/sharew5_rel6-0-0_iv"
global wave_5_mc "`inputw5'/sharew5_rel6-0-0_mc"
global wave_5_mh "`inputw5'/sharew5_rel6-0-0_mh"
global wave_5_ph "`inputw5'/sharew5_rel6-0-0_ph"
global wave_5_sp "`inputw5'/sharew5_rel6-0-0_sp"
global wave_5_xt "`inputw5'/sharew5_rel6-0-0_xt"

*Wave 5 Generated Variables files
global wave_5_gv_health "`inputw5'/sharew5_rel6-0-0_gv_health"
global wave_5_gv_housing "`inputw5'/sharew5_rel6-0-0_gv_housing"
global wave_5_gv_isced "`inputw5'/sharew5_rel6-0-0_gv_isced"
global wave_5_gv_weights "`inputw5'/sharew5_rel6-0-0_gv_weights"
global wave_5_imputations "`inputw5'/sharew5_rel6-0-0_gv_imputations"

*Wave 5 Dropoff files
global wave_5_dropoff "`inputw5'/sharew5_rel6-0-0_dropoff"

*Wave 5 Technical Variables files
global wave_5_techvar "`inputw5'/sharew5_rel6-0-0_technical_variables"

*Wave 6 Capi Modules files
global wave_6_ac "`inputw6'/sharew6_rel6-0-0_ac"
global wave_6_as "`inputw6'/sharew6_rel6-0-0_as"
global wave_6_br "`inputw6'/sharew6_rel6-0-0_br"
global wave_6_cf "`inputw6'/sharew6_rel6-0-0_cf"
global wave_6_ch "`inputw6'/sharew6_rel6-0-0_ch"
global wave_6_co "`inputw6'/sharew6_rel6-0-0_co"
global wave_6_cv_r "`inputw6'/sharew6_rel6-0-0_cv_r"
global wave_6_dn "`inputw6'/sharew6_rel6-0-0_dn"
global wave_6_ep "`inputw6'/sharew6_rel6-0-0_ep"
global wave_6_ex "`inputw6'/sharew6_rel6-0-0_ex"
global wave_6_ft "`inputw6'/sharew6_rel6-0-0_ft"
global wave_6_gs "`inputw6'/sharew6_rel6-0-0_gs"
global wave_6_hc "`inputw6'/sharew6_rel6-0-0_hc"
global wave_6_hh "`inputw6'/sharew6_rel6-0-0_hh"
global wave_6_ho "`inputw6'/sharew6_rel6-0-0_ho"
global wave_6_it "`inputw6'/sharew6_rel6-0-0_it"
global wave_6_iv "`inputw6'/sharew6_rel6-0-0_iv"
global wave_6_mc "`inputw6'/sharew6_rel6-0-0_mc"
global wave_6_mh "`inputw6'/sharew6_rel6-0-0_mh"
global wave_6_ph "`inputw6'/sharew6_rel6-0-0_ph"
global wave_6_sn "`inputw6'/sharew6_rel6-0-0_sn"
global wave_6_sp "`inputw6'/sharew6_rel6-0-0_sp"
global wave_6_xt "`inputw6'/sharew6_rel6-0-0_xt"

*Wave 6 Generated Variables files
global wave_6_gv_health "`inputw6'/sharew6_rel6-0-0_gv_health"
global wave_6_gv_housing "`inputw6'/sharew6_rel6-0-0_gv_housing"
global wave_6_gv_isced "`inputw6'/sharew6_rel6-0-0_gv_isced"
global wave_6_gv_weights "`inputw6'/sharew6_rel6-0-0_gv_weights"
global wave_6_imputations "`inputw6'/sharew6_rel6-0-0_gv_imputations"

*Wave 6 Dropoff files
global wave_6_dropoff "`inputw6'/sharew6_rel6-0-0_dropoff"

*Wave 6 Technical Variables files
global wave_6_techvar "`inputw6'/sharew6_rel6-0-0_technical_variables"


***Prepare Raw Data***
tempfile wave_1_imputations_
use "$wave_1_imputations"
keep if implicat==1
save `wave_1_imputations_', replace
global wave_1_imputations_ `wave_1_imputations_'
clear

tempfile wave_2_imputations_
use "$wave_2_imputations"
keep if implicat==1
save `wave_2_imputations_', replace
global wave_2_imputations_ `wave_2_imputations_'
clear

tempfile wave_4_imputations_
use "$wave_4_imputations"
keep if implicat==1
save `wave_4_imputations_', replace
global wave_4_imputations_ `wave_4_imputations_'
clear

tempfile wave_5_imputations_
use "$wave_5_imputations"
keep if implicat==1
save `wave_5_imputations_', replace
global wave_5_imputations_ `wave_5_imputations_'
clear

tempfile wave_6_imputations_
use "$wave_6_imputations"
keep if implicat==1
save `wave_6_imputations_', replace
global wave_6_imputations_ `wave_6_imputations_'
clear

*generate spouse variables
* There is one program to generate spouse variables

***spouse
***this is a program that creates spouse variables from respondnet information
***
*** the program is called as follows
***		spouse varname, result(result) wave(wave)
***			where:
***				varname - name of respondent variable
***				result 	- name of spouse variable, must be generated before program
***				wave		-	number of the wave
capture program drop spouse
program define spouse
syntax varname, result(varname) wave(integer) [coupleid(varname)]
	if "`coupleid'" == "" {
		local coupleid h`wave'coupid
	}
	replace `result' = .u if w`wave'spouse==0
	replace `result' = .v if w`wave'spouse==2
	bysort `coupleid': replace `result' = `varlist'[_n+1] if !missing(`coupleid') & `varlist'[_n+1] !=. & inw`wave' == 1
	bysort `coupleid': replace `result' = `varlist'[_n-1] if !missing(`coupleid') & `varlist'[_n-1] !=. & inw`wave' == 1
end

*create special missing codes
capture program drop h_wy_level 
program define h_wy_level, rclass
syntax varname [, HRS ELSA SHARE JSTAR CHARLS LASI KLOSA MHAS TILDA CRELES wy(string) asset income ]
    local l = substr("`varlist'",1,1)
    local ll = substr("`varlist'",1,2)
    local lll = substr("`varlist'",1,3)
    if "`ll'" == "hh" {
        local l = substr("`varlist'",1,2)
    }
    if "`lll'" == "inw" {
        local l = substr("`varlist'",1,3)
    }
    if "`l'" == "inw" {
        local t = substr("`varlist'",4,1)
        if "`t'"=="0"|"`t'"=="1"|"`t'"=="2"|"`t'"=="3"|"`t'"=="4"|"`t'"=="5"|"`t'"=="6"|"`t'"=="7"|"`t'"=="8"|"`t'"=="9" {
            local time wave
            local w = substr("`varlist'",4,1)
        	local tt = substr("`varlist'",5,1)
        	if "`tt'"=="0"|"`tt'"=="1"|"`tt'"=="2"|"`tt'"=="3"|"`tt'"=="4"|"`tt'"=="5"|"`tt'"=="6"|"`tt'"=="7"|"`tt'"=="8"|"`tt'"=="9" {
        	    local w = substr("`varlist'",4,2)
        	    local ttt = substr("`varlist'",6,1)
        	    if "`ttt'"=="0"|"`ttt'"=="1"|"`ttt'"=="2"|"`ttt'"=="3"|"`ttt'"=="4"|"`ttt'"=="5"|"`ttt'"=="6"|"`ttt'"=="7"|"`ttt'"=="8"|"`ttt'"=="9" {
        	        local w
        	        local y = substr("`varlist'",4,4)
        	        local time year
        	    }
        	}
    	}
    }
    else if "`l'" == "hh" {
        local t = substr("`varlist'",3,1)
        if "`t'"=="0"|"`t'"=="1"|"`t'"=="2"|"`t'"=="3"|"`t'"=="4"|"`t'"=="5"|"`t'"=="6"|"`t'"=="7"|"`t'"=="8"|"`t'"=="9" {
            local time wave
            local w = substr("`varlist'",3,1)
        	local tt = substr("`varlist'",4,1)
        	if "`tt'"=="0"|"`tt'"=="1"|"`tt'"=="2"|"`tt'"=="3"|"`tt'"=="4"|"`tt'"=="5"|"`tt'"=="6"|"`tt'"=="7"|"`tt'"=="8"|"`tt'"=="9" {
        	    local w = substr("`varlist'",3,2)
        	    local ttt = substr("`varlist'",5,1)
        	    if "`ttt'"=="0"|"`ttt'"=="1"|"`ttt'"=="2"|"`ttt'"=="3"|"`ttt'"=="4"|"`ttt'"=="5"|"`ttt'"=="6"|"`ttt'"=="7"|"`ttt'"=="8"|"`ttt'"=="9" {
        	        local w
        	        local y = substr("`varlist'",3,4)
        	        local time year
        	    }
        	}
    	}
    }
    else {
        local t = substr("`varlist'",2,1)
        if "`t'"=="0"|"`t'"=="1"|"`t'"=="2"|"`t'"=="3"|"`t'"=="4"|"`t'"=="5"|"`t'"=="6"|"`t'"=="7"|"`t'"=="8"|"`t'"=="9" {
            local time wave
            local w = substr("`varlist'",2,1)
        	local tt = substr("`varlist'",3,1)
        	if "`tt'"=="0"|"`tt'"=="1"|"`tt'"=="2"|"`tt'"=="3"|"`tt'"=="4"|"`tt'"=="5"|"`tt'"=="6"|"`tt'"=="7"|"`tt'"=="8"|"`tt'"=="9" {
        	    local w = substr("`varlist'",2,2)
        	    local ttt = substr("`varlist'",4,1)
        	    if "`ttt'"=="0"|"`ttt'"=="1"|"`ttt'"=="2"|"`ttt'"=="3"|"`ttt'"=="4"|"`ttt'"=="5"|"`ttt'"=="6"|"`ttt'"=="7"|"`ttt'"=="8"|"`ttt'"=="9" {
        	        local w
        	        local y = substr("`varlist'",2,4)
        	        local time year
        	    }
        	}
    	}
    }       
    
	return local level "`l'"
	
	if "`w'" == "" & "`y'" == "" {
	    if "`wy'"=="0"|"`wy'"=="1"|"`wy'"=="2"|"`wy'"=="3"|"`wy'"=="4"|"`wy'"=="5"|"`wy'"=="6"|"`wy'"=="7"|"`wy'"=="8"|"`wy'"=="9" | ///
	        "`wy'"=="10"|"`wy'"=="11"|"`wy'"=="12"|"`wy'"=="13"|"`wy'"=="14"|"`wy'"=="15"|"`wy'"=="16"|"`wy'"=="17"|"`wy'"=="18"|"`wy'"=="19" {
		    local w `wy'
		}
		else {
		    local y `wy'
		}
		local time panel
	}
	local studies `hrs' `elsa' `share' `jstar' `charls' `lasi' `klosa' `mhas' `tilda'
	local n_studies : word count `studies'
	if "`w'"=="0"|"`w'"=="1"|"`w'"=="2"|"`w'"=="3"|"`w'"=="4"|"`w'"=="5"|"`w'"=="6"|"`w'"=="7"|"`w'"=="8"|"`w'"=="9" | ///
	        "`w'"=="10"|"`w'"=="11"|"`w'"=="12"|"`w'"=="13"|"`w'"=="14"|"`w'"=="15"|"`w'"=="16"|"`w'"=="17"|"`w'"=="18"|"`w'"=="19" {
		if `n_studies' > 1 {
			di "can only specify one study"
			exit 198
		}
		else if "`hrs'" == "hrs" {
			local y = 1992 + ((`w'-1)*2)
		}
		else if "`elsa'" == "elsa" {
			local y = 2002 + ((`w'-1)*2)
		}
		else if "`share'" == "share" {
			local y = 2004 + ((`w'-1)*2)
		}
		else if "`jstar'" == "jstar" {
			local y = 2006 + ((`w'-1)*2)
		}
		else if "`charls'" == "charls" {
			local y = 2010 + ((`w'-1)*2)
		}
		else if "`lasi'" == "lasi" {
			local y = 2012 + ((`w'-1)*2)
		}
		else if "`klosa'" == "klosa" {
			local y = 2006 + ((`w'-1)*2)
		}
		else if "`mhas'" == "mhas" {
		    if `w' == 1 | `w' == 2 {
			    local y = 2000 + ((`w'-1)*2)
			}
			else {
			    local y = 2012 + ((`w'-1)*2)
			}
		}
		else if "`tilda'" == "tilda" {
			local y = 2010 + ((`w'-1)*2)
		}
		else if "`creles'" == "creles" {
		    if `w' == 1 | `w' == 2 | `w' == 3 {
			    local y = 2004 + ((`w'-1)*2)
			}
			else {
			    local y = 2010 + ((`w'-1)*2)
			}
		}
		return local wy `w'
	}
	else if "`y'" != "" {
		if `n_studies' > 1 {
			di "can only specify one study"
			exit 198
		}
		else if "`hrs'" == "hrs" {
			local w = ((`y'-1992)/2)+1
		}
		else if "`elsa'" == "elsa" {
			local w = ((`y'-2002)/2)+1
		}
		else if "`share'" == "share" {
			local w = ((`y'-2004)/2)+1
		}
		else if "`jstar'" == "jstar" {
			local w = ((`y'-2006)/2)+1
		}
		else if "`charls'" == "charls" {
			local w = ((`y'-2010)/2)+1
		}
		else if "`lasi'" == "lasi" {
			local w = ((`y'-2012)/2)+1
		}
		else if "`klosa'" == "klosa" {
			local w = ((`y'-2006)/2)+1
		}
		else if "`mhas'" == "mhas" {
		    if `y' == 2000 | `y' == 2002 {
			    local w = ((`y'-2000)/2)+1
			}
			else {
			    local w = ((`y'-2012)/2)+1
			}
		}
		else if "`tilda'" == "tilda" {
			local w = ((`y'-2010)/2)+1
		}
		else if "`creles'" == "creles" {
		    if `y' == 2004 | `y' == 2006 | `y' == 2008 {
			    local w = ((`y'-2004)/2)+1
			}
			else {
			    local w = ((`y'-2010)/2)+1
			}
		}
		return local wy `y'
	}
	
	if "`asset'" == "asset" {
	    if "`hrs'" == "hrs" {
			local fin_time this_year 
		}
		else if "`elsa'" == "elsa" {
			local fin_time this_year 
		}
		else if "`share'" == "share" {
			local fin_time this_year 
		}
		else if "`jstar'" == "jstar" {
			local fin_time this_year 
		}
		else if "`charls'" == "charls" {
			local fin_time this_year
		}
		else if "`lasi'" == "lasi" {
			local fin_time this_year 
		}
		else if "`klosa'" == "klosa" {
			local fin_time this_year 
		}
		else if "`mhas'" == "mhas" {
			local fin_time this_year 
		}
		else if "`tilda'" == "tilda" {
			local fin_time this_year 
		}
		else if "`creles'" == "creles" {
			local fin_time this_year 
		}
	}
	else if "`income'" == "income" {
	    if "`hrs'" == "hrs" {
	        if `w' == 1 {
	            local fin_time sp_year
	            local fin_sp_year = 1991
	        }
	        else if `w' == 2 {
	            local fin_time mixed
	            local fin_sp_year = 1993
	        }
	        else {
	            local fin_time last_year
			}
		}
		else if "`elsa'" == "elsa" {
			local fin_time this_year 
		}
		else if "`share'" == "share" {
			local fin_time last_year
		}
		else if "`jstar'" == "jstar" {
			local fin_time last_year
		}
		else if "`charls'" == "charls" {
			local fin_time last_year
		}
		else if "`lasi'" == "lasi" {
			local fin_time this_year
		}
		else if "`klosa'" == "klosa" {
			local fin_time last_year
		}
		else if "`mhas'" == "mhas" {
			local fin_time unknown
		}
		else if "`tilda'" == "tilda" {
			local fin_time unknown
		}
		else if "`creles'" == "creles" {
			local fin_time unknown
		}
	}
	
	return local wave `w'
	return local year `y'
	return local time `time'
	return local fin_time `fin_time'
	return local fin_sp_year `fin_sp_year'
end
* There are three programs to genearte missing values
*					1. missing_common
*					2. missing_financial
*					4. missing_h

***missing_common
***this is a program that creates speical missing codes for SHARE common variables
***
*** the program is called as follows
***		missing_common varlist [if] [in], result(result)
***			where:
***				varlist - list of variables which should influnce missing codes
***				result 	- name of variable, must be generated before program
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop missing_common
program define missing_common
syntax varlist [if] [in], result(varname) [wave(string)]

    marksample touse, novarlist // process if and in statements
    if "`wave'" == "" {
        h_wy_level `result'
        local w `r(wave)'
        local time `r(time)'
    }
    else {
        local w `wave'
        local time wave
    }
    
    quietly {
        if "`time'" == "wave" {
            foreach v of varlist `varlist' {
        		replace `result' = .m if `v' == . & inw`w' == 1 & (`touse') // this is the lowest category
        	}
        }
    	foreach v of varlist `varlist' {
    		replace `result' = .d if `v' == -1  & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `result' = .r if `v' == -2 & (`touse')
    	}
    }
end

***missing_financial
***this is a program that creates speical missing codes for SHARE financial variables
***
*** the program is called as follows
***		missing_financial varlist, result(result)
***			where:
***				varlist - list of variables which should influnce missing codes
***				result 	- name of variable, must be generated before program
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop missing_financial
program define missing_financial
syntax varlist [if] [in], result(varname)

    marksample touse, novarlist // process if and in statements
    
    quietly {
    	foreach v of varlist `varlist' {
    		replace `result' = .d if `v' == -9999991  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `result' = .r if `v' == -9999992 & (`touse')
    	}
    }
end

***missing_h
***this is a program that creates speical missing codes for Harmonized variables
***
*** the program is called as follows
***		missing_h varlist, result(result)
***			where:
***				varlist - list of harmonized variables which should influnce missing codes
***				result 	- name of variable, must be generated before program
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop missing_h
program define missing_h
syntax varlist [if] [in], result(varname)

marksample touse, novarlist // process if and in statements

quietly {
	foreach v of varlist `varlist' {
		replace `result' = .m if `v' == .m  & (`touse') // this is the lowest category
		}
	foreach v of varlist `varlist' {
		replace `result' = .d if `v' == .d & (`touse')
		}
	foreach v of varlist `varlist' {
		replace `result' = .r if `v' == .r & (`touse')
		}
	}
end

*create asset flag varibles for Wave 4 imputations
***w1_imputation_flag
***this is program that combines imputation flags from multiple wave 1 financial variables.
***			this flag program makes these categorical transformations
***       0.Not imputed         to 0.no imputation
***       1.imputed.            to 1.imputation
***
*** the program is called as follows
***		w1_imputation_flag varlist [if] [in], gen(varname)
***			where:
***				varlist - list of the multiple imputation flag variables to be combined
***				gen - the name of the desired combined flag variable which must be pre-generated
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop w1_imputation_flag
program define w1_imputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
        	foreach v of varlist `varlist' {
	            gen hp`i'f`v' = .
	            gen hm`i'f`v' = .
	            bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if !mi(`by')
	            bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if !mi(`by')
	            local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
	          }
        }
        local varlist `varlist' `hhvars'
    }

    quietly {
    	foreach v of varlist `varlist' {
    		replace `gen' = 0 if `v' == 3  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 1 if `v' == 14 & (`touse')
    	}
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
end

***w1_isaimputation_flag
***this is program that combines imputation flags from Israel wave 1 non-housing asset variables.
***			this flag program makes these categorical transformations
***				1.Continuous answer				  to 0.no imputation
***				2.Complete Bracket				  to 1.imputation
***	            3.Incomplete Bracket			  to 1.imputation
***	            5.Refuse bracket sequence         to 1.imputation
***	            6.Doesn't own the item		      to 0.no imputation
***	            7.Refusal/DK ownership		      to 1.imputation
***	            9.Not financial respondent        to .(missing)
***	            10.Negative/implausible			  to 1.imputation
***
*** the program is called as follows
***		w1_isaimputation_flag varlist [if] [in], gen(varname) by(varname)
***			where:
***				varlist - list of the flags to be combined
***				gen - the name of the desired combined flag variable
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop w1_isaimputation_flag
program define w1_isaimputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
        	foreach v of varlist `varlist' {
	            gen hp`i'f`v' = .
	            gen hm`i'f`v' = .
	            bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if !mi(`by')
	            bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if !mi(`by')
	            local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
	          }
        }
        local varlist `varlist' `hhvars'
    }
    quietly {
    	foreach v of varlist `varlist' {
    		replace `gen' = 0 if (`v' == 1 | `v' == 6)  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 1 if (`v' == 2 | `v' == 3 | `v' == 5 | `v' == 7 | `v' == 10 | (h1anyfin == 0 & country == 25)) & (`touse')
    	}
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
    
end

***w1_ishaimputation_flag
***this is program that combines imputation flags from Israel wave 1 housing asset variables.
***			this flag program makes these categorical transformations
***				0.Doesn't own the item				to 0.no imputation
***				1.No imputation				        to 0.no imputation
***	            5.Some imputations			        to 1.imputation
***	            9.No housing section respodent      to 1.imputation
***
*** the program is called as follows
***		w1_ishaimputation_flag varlist [if] [in], gen(varname) by(varname)
***			where:
***				varlist - list of the flags to be combined
***				gen - the name of the desired combined flag variable
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop w1_ishaimputation_flag
program define w1_ishaimputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
        	foreach v of varlist `varlist' {
	            gen hp`i'f`v' = .
	            gen hm`i'f`v' = .
	            bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if !mi(`by')
	            bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if !mi(`by')
	            local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
	          }
        }
        local varlist `varlist' `hhvars'
    }
    quietly {
    	foreach v of varlist `varlist' {
    		replace `gen' = 0 if (`v' == 0 | `v' == 1)  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 1 if (`v' == 5 | `v' == 9) & (`touse')
    	}
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
    
end

***w1_isiimputation_flag
***this is program that combines imputation flags from Israel wave 1 income variables.
***			this flag program makes these categorical transformations
***				0.Does not apply				  to 0.no imputation
***				1.No imputations				  to 0.no imputation
***	            5.Some imputations			      to 1.imputation
***
*** the program is called as follows
***		w1_isiimputation_flag varlist [if] [in], gen(varname) by(varname)
***			where:
***				varlist - list of the flags to be combined
***				gen - the name of the desired combined flag variable
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop w1_isiimputation_flag
program define w1_isiimputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
        	foreach v of varlist `varlist' {
	            gen hp`i'f`v' = .
	            gen hm`i'f`v' = .
	            bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if !mi(`by')
	            bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if !mi(`by')
	            local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
	          }
        }
        local varlist `varlist' `hhvars'
    }
    quietly {
    	foreach v of varlist `varlist' {
    		replace `gen' = 0 if (`v' == 0 | `v' == 1)  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 1 if `v' == 5 & (`touse')
    	}
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
    
end

***w2_imputation_flag
***this is program that combines imputation flags from multiple wave 2 variables.
***			this flag program makes these categorical transformations
***       0.Not imputed         to 0.no imputation
***       1.imputed.            to 1.imputation
***
*** the program is called as follows
***		w2_imputation_flag varlist [if] [in], gen(varname)
***			where:
***				varlist - list of the multiple imputation flag variables to be combined
***				gen - the name of the desired combined flag variable which must be pre-generated
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop w2_imputation_flag
program define w2_imputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
        	foreach v of varlist `varlist' {
	            gen hp`i'f`v' = .
	            gen hm`i'f`v' = .
	            bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if !mi(`by')
	            bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if !mi(`by')
	            local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
	          }
        }
        local varlist `varlist' `hhvars'
    }

    quietly {
    	foreach v of varlist `varlist' {
    		replace `gen' = 0 if `v' == 0  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 1 if `v' == 1 & (`touse')
    	}
    	replace `gen' = -9 if country == 30 & inw2 ==1 
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
end

***w4_chimputation_flag
***this is program that combines imputation flags from multiple wave 4 child variables.
***			this flag program makes these categorical transformations
***       1.Not designated resp         to .(missing)
***       3.Regular obs.                to 0.Not imputed
***      14.Imp: missing value          to 1.Imputed
***
*** the program is called as follows
***		w4_chimputation_flag varlist [if] [in], gen(varname)
***			where:
***				varlist - list of the multiple imputation flag variables to be combined
***				gen - the name of the desired combined flag variable which must be pre-generated
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop w4_chimputation_flag
program define w4_chimputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
        	foreach v of varlist `varlist' {
	            gen hp`i'f`v' = .
	            gen hm`i'f`v' = .
	            bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if !mi(`by')
	            bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if !mi(`by')
	            local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
	          }
        }
        local varlist `varlist' `hhvars'
    }

    quietly {
    	foreach v of varlist `varlist' {
    		replace `gen' = 0 if `v' == 3  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 1 if `v' == 14 & (`touse')
    	}
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
end

***w4_imputation_flag
***this is program that combines imputation flags from multiple wave 4 financial variables.
***			this flag program makes these categorical transformations
***     -99.missing by design       to -3.institutional interview
***       1.not designed respondent to .(missing)
***       2.No ownership            to 6.no asset/income
***       3.Regular obs.            to 1.continuous value
***       4.Imp: ub point           to 2.complete bracket
***       5.Imp: ub range           to 2.complete bracket
***       6.Imp: ub incomplete      to 3.incomplete bracket
***       7.Imp: ub uniformative    to 5.no value/bracket
***       8.Imp: ownership          to 7.DK ownership/if income
***       9.Imp: ammount            to 5.no value/bracket
***      10.Imp: outlier LB         to 5.no value/bracket
***      11.Imp: outlier UB         to 5.no value/bracket
***      12.Imp: aggregate          to .(missing)
***      13.Imp: NRP                to 9.non-responding spouse
***      14.Imp: missing value      to 5.no value/bracket
***
*** the program is called as follows
***		w4_imputation_flag varlist [if] [in], gen(varname) by(varname)
***			where:
***				varlist - list of the financial flags to be combined
***				gen - the name of the desired combined flag variable
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop w4_imputation_flag
program define w4_imputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
        	foreach v of varlist `varlist' {
	            gen hp`i'f`v' = .
	            gen hm`i'f`v' = .
	            bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if !mi(`by')
	            bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if !mi(`by')
	            local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
	          }
        }
        local varlist `varlist' `hhvars'
    }
    quietly {
    	foreach v of varlist `varlist' {
    		replace `gen' = 6 if (`v' == 2)  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 1 if `v' == 3 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 2 if (`v' == 4 | `v' == 5)  & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 3 if `v' == 6 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 5 if (`v' == 7 | `v' == 9 | `v' == 10 | `v' == 11 | `v' == 14) & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 7 if `v' == 8 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 9 if `v' == 13 & (`touse')
    	}
    	foreach v of varlist `varlist' {
		    replace `gen' = -3 if `v' == -99 & (`touse')
			}
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
    
end

***chimputation_flag
***this is program that combines imputation flags from multiple wave children 5 variables.
***			this flag program makes these categorical transformations
***       1.Not designated resp         to .(missing)
***       3.Regular obs.                to 0.Not imputed
***      14.Imp: missing value          to 1.Imputed
***
*** the program is called as follows
***		chimputation_flag varlist [if] [in], gen(varname)
***			where:
***				varlist - list of the multiple imputation flag variables to be combined
***				gen - the name of the desired combined flag variable which must be pre-generated
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop chimputation_flag
program define chimputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
        	foreach v of varlist `varlist' {
	            gen hp`i'f`v' = .
	            gen hm`i'f`v' = .
	            bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if `by' == `by'[_n+`i'] & !mi(`by')
	            bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if `by' == `by'[_n-`i'] & !mi(`by')
	            local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
	          }
        }
        local varlist `varlist' `hhvars'
    }

    quietly {
    	foreach v of varlist `varlist' {
    		replace `gen' = 0 if `v' == 3  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 1 if `v' == 14 & (`touse')
    	}
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
end

***w5_imputation_flag
***this is program that combines imputation flags from multiple wave 5 financial variables.
***			this flag program makes these categorical transformations
***     -99.missing by design       to -3.institutional interview
***       1.not designed respondent to .(missing)
***       2.No ownership            to 6.no asset/income
***       3.Regular obs.            to 1.continuous value
***       4.Imp: ub point           to 2.complete bracket
***       5.Imp: ub range           to 2.complete bracket
***       6.Imp: ub incomplete      to 3.incomplete bracket
***       7.Imp: ub uniformative    to 5.no value/bracket
***       8.Imp: ownership          to 7.DK ownership/if income
***       9.Imp: ammount            to 5.no value/bracket
***      10.Imp: outlier LB         to 5.no value/bracket
***      11.Imp: outlier UB         to 5.no value/bracket
***      12.Imp: aggregate          to .(missing)
***      13.Imp: NRP                to 9.non-responding spouse
***      14.Imp: missing value      to 5.no value/bracket
***
*** the program is called as follows
***		w5_imputation_flag varlist [if] [in], gen(varname) by(varname)
***			where:
***				varlist - list of the financial flags to be combined
***				gen - the name of the desired combined flag variable
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop w5_imputation_flag
program define w5_imputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
        	foreach v of varlist `varlist' {
	            gen hp`i'f`v' = .
	            gen hm`i'f`v' = .
	            bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if !mi(`by')
	            bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if !mi(`by')
	            local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
	          }
        }
        local varlist `varlist' `hhvars'
    }
    quietly {
    	foreach v of varlist `varlist' {
    		replace `gen' = 6 if (`v' == 2)  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 1 if `v' == 3 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 2 if (`v' == 4 | `v' == 5)  & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 3 if `v' == 6 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 5 if (`v' == 7 | `v' == 9 | `v' == 10 | `v' == 11 | `v' == 14) & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 7 if `v' == 8 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 9 if `v' == 13 & (`touse')
    	}
    	foreach v of varlist `varlist' {
		    replace `gen' = -3 if `v' == -99 & (`touse')
			}
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
    
end

***w6_imputation_flag
***this is program that combines imputation flags from multiple wave 6 financial variables.
***         this flag program makes these categorical transformations
***     -99.missing by design       to -3.institutional interview
***       1.not designed respondent to .(missing)
***       2.No ownership            to 6.no asset/income
***       3.Regular obs.            to 1.continuous value
***       4.Imp: ub point           to 2.complete bracket
***       5.Imp: ub range           to 2.complete bracket
***       6.Imp: ub incomplete      to 3.incomplete bracket
***       7.Imp: ub uniformative    to 5.no value/bracket
***       8.Imp: ownership          to 7.DK ownership/if income
***       9.Imp: ammount            to 5.no value/bracket
***      10.Imp: outlier LB         to 5.no value/bracket
***      11.Imp: outlier UB         to 5.no value/bracket
***      12.Imp: aggregate          to .(missing)
***      13.Imp: NRP                to 9.non-responding spouse
***      14.Imp: missing value      to 5.no value/bracket
***
*** the program is called as follows
***     w6_imputation_flag varlist [if] [in], gen(varname) by(varname)
***         where:
***             varlist - list of the financial flags to be combined
***             gen - the name of the desired combined flag variable
***             [if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop w6_imputation_flag
program define w6_imputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
            foreach v of varlist `varlist' {
                gen hp`i'f`v' = .
                gen hm`i'f`v' = .
                bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if !mi(`by')
                bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if !mi(`by')
                local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
              }
        }
        local varlist `varlist' `hhvars'
    }
    quietly {
        foreach v of varlist `varlist' {
            replace `gen' = 6 if (`v' == 2)  & (`touse') // this is the lowest category
        }
        foreach v of varlist `varlist' {
            replace `gen' = 1 if `v' == 3 & (`touse')
        }
        foreach v of varlist `varlist' {
            replace `gen' = 2 if (`v' == 4 | `v' == 5)  & (`touse')
        }
        foreach v of varlist `varlist' {
            replace `gen' = 3 if `v' == 6 & (`touse')
        }
        foreach v of varlist `varlist' {
            replace `gen' = 5 if (`v' == 7 | `v' == 9 | `v' == 10 | `v' == 11 | `v' == 14) & (`touse')
        }
        foreach v of varlist `varlist' {
            replace `gen' = 7 if `v' == 8 & (`touse')
        }
        foreach v of varlist `varlist' {
            replace `gen' = 9 if `v' == 13 & (`touse')
        }
        foreach v of varlist `varlist' {
            replace `gen' = -3 if `v' == -99 & (`touse')
            }
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
    
end

***imputation_flag
***this is program that combines imputation flags from multiple wave 5 financial variables.
***			this flag program makes these categorical transformations
***     -99.missing by design       to -3.institutional interview
***       1.not designed respondent to .(missing)
***       2.No ownership            to 6.no asset/income
***       3.Regular obs.            to 1.continuous value
***       4.Imp: ub point           to 2.complete bracket
***       5.Imp: ub range           to 2.complete bracket
***       6.Imp: ub incomplete      to 3.incomplete bracket
***       7.Imp: ub uniformative    to 5.no value/bracket
***       8.Imp: ownership          to 7.DK ownership/if income
***       9.Imp: ammount            to 5.no value/bracket
***      10.Imp: outlier LB         to 5.no value/bracket
***      11.Imp: outlier UB         to 5.no value/bracket
***      12.Imp: aggregate          to .(missing)
***      13.Imp: NRP                to 9.non-responding spouse
***      14.Imp: missing value      to 5.no value/bracket
***
*** the program is called as follows
***		imputation_flag varlist [if] [in], gen(varname) by(varname)
***			where:
***				varlist - list of the financial flags to be combined
***				gen - the name of the desired combined flag variable
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop imputation_flag
program define imputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
        	foreach v of varlist `varlist' {
	            gen hp`i'f`v' = .
	            gen hm`i'f`v' = .
	            bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if !mi(`by')
	            bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if !mi(`by')
	            local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
	          }
        }
        local varlist `varlist' `hhvars'
    }
    quietly {
    	foreach v of varlist `varlist' {
    		replace `gen' = 6 if (`v' == 2)  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 1 if `v' == 3 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 2 if (`v' == 4 | `v' == 5)  & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 3 if `v' == 6 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 5 if (`v' == 7 | `v' == 9 | `v' == 10 | `v' == 11 | `v' == 14) & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 7 if `v' == 8 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 9 if `v' == 13 & (`touse')
    	}
    	foreach v of varlist `varlist' {
		    replace `gen' = -3 if `v' == -99 & (`touse')
			}
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
    
end


***h_simple_imputation_flag
***this is program that combines harmonized simple imputation flags
***			this flag program assumes that categories are and will remain:
***				-3.institutional interview
***				-2.non-responding spouse 
***       -1.multiple econ units in hh
***        0.no imputation
***        1.imputation
***
*** the program is called as follows
***		h_simple_imputation_flag varlist [if] [in], gen(varname)
***			where:
***				varlist - list of the multiple imputation flag variables to be combined
***				gen - the name of the desired combined flag variable which must be pre-generated
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop h_simple_imputation_flag
program define h_simple_imputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
        	foreach v of varlist `varlist' {
	            gen hp`i'f`v' = .
	            gen hm`i'f`v' = .
	            bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if !mi(`by')
	            bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if !mi(`by')
	            local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
	          }
        }
        local varlist `varlist' `hhvars'
    }

    quietly {
    	foreach v of varlist `varlist' {
    		replace `gen' = 0 if `v' == 0  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 1 if `v' == 1 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = -1 if `v' == -1 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = -2 if `v' == -2 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = -3 if `v' == -3 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = -9 if `v' == -9 & (`touse')
    	}
    	
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
end



***h_imputation_flag
***this is program that combines harmonized imputation flags
***			this flag program assumes that categories are and will remain:
***				-3.institutional interview
***				-2.non-responding spouse 
***       -1.multiple econ units in hh
***        1.continuous value
***        2.complete bracket
***        3.incomplete bracket
***        5.no value/bracket
***        6.no asset/income
***        7.dk ownership
***        9.non-responding spouse 

***
*** the program is called as follows
***		h_imputation_flag varlist [if] [in], gen(varname)
***			where:
***				varlist - list of the multiple imputation flag variables to be combined
***				gen - the name of the desired combined flag variable which must be pre-generated
***				[if] and [in] allow limitation of the program to a subsample using an if or in statement, both are optional
capture program drop h_imputation_flag
program define h_imputation_flag
syntax varlist [if] [in], gen(string) [by(varname)]

    marksample touse, novarlist // process if and in statements
    qui gen byte `gen' = .
    
    if "`by'" != "" {
        qui bysort `by': gen pid = _n if !mi(`by')
        qui summarize pid
        local hhvars
        local mymax = `r(max)'-1
        qui forvalues i = 1/`mymax' { 
        	foreach v of varlist `varlist' {
	            gen hp`i'f`v' = .
	            gen hm`i'f`v' = .
	            bysort `by': replace hp`i'f`v' = `v'[_n+`i'] if !mi(`by')
	            bysort `by': replace hm`i'f`v' = `v'[_n-`i'] if !mi(`by')
	            local hhvars `hhvars' hp`i'f`v' hm`i'f`v'
	          }
        }
        local varlist `varlist' `hhvars'
    }

    quietly {
    	foreach v of varlist `varlist' {
    		replace `gen' = 6 if `v' == 6  & (`touse') // this is the lowest category
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 1 if `v' == 1 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 2 if `v' == 2 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 3 if `v' == 3 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 5 if `v' == 5 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 7 if `v' == 7 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = 9 if `v' == 9 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = -1 if `v' == -1 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = -2 if `v' == -2 & (`touse')
    	}
    	foreach v of varlist `varlist' {
    		replace `gen' = -3 if `v' == -3 & (`touse')
    	}
    	
    }
    if "`by'" != "" {
        drop `hhvars' pid
    }
end

*impute missing values with hotdeck imputation
program define hotdeckvar, sortpreserve byable(recall)
*! 1.0.3  Feb  6, 2003 Matthias Schonlau 
*! 1.0.4  May 13, 2003 adding "by" option
*! 1.0.5  Jan 19, 2005 in case of duplicates a stable sort was required to ensure that the seed works properly
*! 1.0.6  Feb  9, 2006 Multivariate imputation, i.e. miss values in one obs are always impute from the same donor obs
*! 1.0.7  Feb 17, 2008 ignore option
*! 1.1.0  Apr  6, 2016 updated to version 14.0 ("set seed" no longer functioned with uniform() under version 7) 
	version 13.0
	syntax [varlist] [if] [in] ,  [ SUffix(str) ignore ]

	/* replaces missing values by random values from the same variable and from 
	observations allowed by if and in */


	* observations to use , keep missing value
	marksample touse , novarlist
	qui count if `touse' 
	if r(N) == 0 { 
		error 2000 
	} 
	local n_touse=r(N)
	*display "n_touse `n_touse' " 
	local k : word count `varlist'
	tokenize "`varlist'"

	if (`k'==0) {
		di as err "Need at least one variable"
		error 11
	}

	if (length("`suffix'")==0) {
		local suffix "_i"
	}
	tempname z  
	tempvar temp_sort   
	* for stable sorting 
	qui gen `temp_sort'=.


	tempvar nmiss impute_pool bstr nmiss_i 
	forval i = 1 / `k' { 
		local z`i'="``i''" 
		*display "z= `z`i''"
	}


	* separate data to impute from dis-allowed data
	gen `impute_pool'= 1
	qui replace `impute_pool'=0 if !`touse'
	forval i = 1 / `k' { 
		qui replace `impute_pool'=0 if  `z`i''==.  
	}

	qui sum `impute_pool'
	local sum1 =r(sum) 
	di as res  "Number of observations without missing values:" `sum1'
	local   nmiss= `n_touse' - `sum1'
	display as res  "Number of observations with    missing values:" `nmiss' 
	if (`n_touse'==`nmiss') { 
		if (!_by()) {
			di as error  "Error: There are missing values in each observation."
			di as error  "The variables cannot be imputed with a multivariate hotdeck."
			if ("`ignore'"=="") {
				error 10
			}
		}
		else  {
			di as error "Error: This by-group has missing values for each observation and cannot be imputed."
			if ("`ignore'"=="") {
				error 10
			}
		}	
	}
	if (`sum1'<`nmiss') { 
		di as error  "Warning: More missing values than observations without missing values."	
	}

	forval i = 1 / `k' { 
		local z_`i'="`z`i''`suffix'" 
		if ( (!_by() & `nmiss'>0) | ( _by() & _byindex()==1) ) {
			* if _by() we need to generate variable 
			* in case first group doesn't have missing values but later group does
			qui gen `z_`i''=``i''  /* copy entire variable, including not `touse'*/
			local g : value label ``i''
			label values `z_`i'' `g'
			local gg : variable label ``i''
			label variable `z_`i'' "`gg'"
		}
	}


	if (`nmiss'>0) { 
			* sort values that may be used for to replace missing values to the end of the data set
			* gsort -`impute_pool' substitute gsort with a stable sort 
			sort `impute_pool', stable
			qui replace `temp_sort'= _n
			gsort - `temp_sort'

			qui sum `impute_pool'
			local n_impute_pool=r(sum) 
			*display " n_impute_pool = `n_impute_pool' " 
			* impute using observatiosns  from 1.. n_impute_pool 
			qui gen long `bstr' = int(runiform()* `n_impute_pool') +1 
			forval i = 1 / `k' { 
				disp as result "Imputing `z_`i''"
				* replace from the same observation, but only replace is actually missing
				replace `z_`i''=`z`i''[`bstr'] if `z`i''==. & `touse'  /* only impute `touse' missing values*/		
				qui egen `nmiss_i'=sum(`z_`i''==. & `touse') /* possible error only for `touse' values'*/
				if (`nmiss_i'>0 & `nmiss_i'!=.) { 
					display as error "Number of missing in imputed variable `z_`i'' = " `nmiss_i'
					di as error "Imputed variable still has missing values"
					if (`ignore'=="") {
						error 9
					}
				}
				drop `nmiss_i'
			}

			* get rid of imputed varialbes where there were no missing data
			if (_bylastcall()) {
				forval i = 1 / `k' { 
					qui count if missing(`z`i'')
					if r(N)==0 {
						drop  `z_`i''
					} 
				}
			}



	}
	else {
			if (!_by()) {
				di as res "There are no missing values."
			}
			else  {
				di as res "This by-group has no missing values."
			}
		
	}

	
 
end 


capture program drop share_hotdeck
program define share_hotdeck, sort
syntax varname [if] [in], level(string) wave(int) [entry_var(varname) ///
                          bv1_var(varname) bv2_var(varname) bv3_var(varname) ///
                          bounds_var(varname) list extralistvars(passthru) exclude_values(string) ]
    marksample touse, novarlist 
    
    qui gen `varlist'_i = .
    qui gen `varlist'_i_f = .
    
    
    if "`level'" == "respondent" {
        local condopt conds(isocountry ragender_i r`wave'agecat_i)
    }
    else if "`level'" == "couple" {
        local condopt conds(isocountry h`wave'butype_i h`wave'agecat_i)
    }
    else if "`level'" == "household" {
        local condopt conds(isocountry hh`wave'butype_i hh`wave'agecat_i)
    }
    else {
        di as error "level must either be set as respodent, couple, or household"
    }
    
    if "`bv1_var'" != "" & "`bv2_var'" != "" & "`bv3_var'" != "" & "`bounds_var'" != "" {
        tempvar mt_var lt_var
        qui gen `mt_var' = .
        qui gen `lt_var' = .
        
        parse_share_bracketvars `varlist', bv1_var(`bv1_var') bv2_var(`bv2_var') bv3_var(`bv3_var') bounds_var(`bounds_var') lt_var(`lt_var') mt_var(`mt_var')
        
        local maxminopt min(`mt_var') max(`lt_var') min_operator(>) max_operator(<)
    }
    
    if "`exclude_values'" != "" {
        local excludeopt exclude_values(`exclude_values')
    }
    
    if "`entry_var'" != "" {
        *share_entry_check `varlist', entryvar(`entry_var') `extralistvars'
        
        //impute if postive ownership
        harmonized_hotdeck `varlist' if `touse' & `entry_var' == 1 & inw`wave' == 1, harmonized(`varlist'_i) ///
                           `maxminopt' `condopt' exclude_flags(2,3,5,6,7) `excludeopt' wave(`wave') `list' `extralistvars'

        //assign 0 values if negative ownership
        qui replace `varlist'_i = 0 if `entry_var' == 0
        qui replace `varlist'_i_f = 6 if `entry_var' == 0
        
        //impute if ownership is unknown
        harmonized_hotdeck `varlist' if `touse' & inw`wave' == 1, dkown harmonized(`varlist'_i) ///
                           `condopt' exclude_flags(2,3,5,7) `excludeopt' wave(`wave') `list' `extralistvars'
    }
    else {
        //impute (no ownership question)
        harmonized_hotdeck `varlist' if `touse' & inw`wave' == 1, dkown harmonized(`varlist'_i) ///
                           `maxminopt' `condopt' exclude_flags(2,3,5,7) `excludeopt' wave(`wave') `list' `extralistvars'
    }
    
    imputation_summary_table `varlist'_i if `touse' & inw`wave' == 1, `maxminopt'
    
end
     capture program drop harmonized_hotdeck
program define harmonized_hotdeck, sort
syntax varname [if] [in], harmonized(varname) conds(varlist) ///
            [min(varname) max(varname)  min_operator(string) max_operator(string) ///
            dkown exclude_flags(passthru) exclude_values(string) noSeed list extralistvars(varlist) wave(string) ]
	marksample touse, novarlist 
	
	if "`seed'" == "" {
    	set seed 123456789
        *set seed X075bcd151f123bb5159a55e50022865700043e55
	}
	
	capture drop group
	capture drop cell
    
    if "`wave'" == "" {
        h_wy_level `harmonized'
        local w `r(wave)'
        if "`w'" == "" {
            di as error "must set wave number"
            exit 198
        }
    }
    else {
        local w `wave'
    }
    
        
    
    if "`exclude_values'" != "" {
	    local exclude_cond & !inlist(`varlist',`exclude_values')
	}
	else {
	    local exclude_cond
	}
    
    qui replace `harmonized' = `varlist' if !mi(`varlist') & `touse' & mi(`harmonized') `exclude_cond'
    qui replace `harmonized'_f = 1 if !mi(`varlist') & `touse' & mi(`harmonized'_f) `exclude_cond'
    qui count if `touse' & inw`w'==1 & mi(`harmonized')
    if `r(N)' > 0 {
        if "`min'" != "" | "`max'" != "" {
            qui egen group = group(`conds') if `touse' & inw`w'==1 & mi(`harmonized')
            
            su group, meanonly
        	forvalues i = 1/`r(max)' {
        	    
        	    di
        	    local cond_state
        	    foreach var of local conds {
            	    qui sum `var' if group == `i'
            	    if "`r(mean)'" != "" {
            	        local cmean = `r(mean)'
            	    }
            	    else {
            	        local cmean = .
            	    }
            	    di as result "`var'=`cmean'"
            	    local cond_state `cond_state' & `var' == `cmean'
            	}
            	qui count if `touse' `cond_state' & inw`w'==1
            	di as result "`r(N)' respondents in this group"
            	qui egen cell = group(`min' `max') if `touse' `cond_state' & inw`w'==1 & mi(`harmonized'), missing
                
            	su cell, meanonly
            	forvalues i = 1/`r(max)' {
            		su `min' if cell == `i', meanonly
            		local bracket_min = r(mean)
            		su `max' if cell == `i', meanonly    
            		local bracket_max = r(mean)
            		di
            		di as result "reported min=`bracket_min' max=`bracket_max'"
            		
            		if "`bracket_min'" != "." {
            			if "`bracket_max'" != "." {
            				hotdeck_impute `harmonized' if `touse' `cond_state', ///
            				    min(`bracket_min') max(`bracket_max') min_operator(`min_operator') max_operator(`max_operator') ///
            					imput_flag(`harmonized'_f) flag_value(2)  cell(`i') `exclude_flags' conds(`conds') `list'
            			}
            			else {
        					hotdeck_impute `harmonized' if `touse' `cond_state', ///
        					    min(`bracket_min') min_operator(`min_operator') ///
        						imput_flag(`harmonized'_f) flag_value(3)  cell(`i') `exclude_flags' conds(`conds') `list'
            			}
            		}
            		else {
            			if "`bracket_max'" != "." {
            			    hotdeck_impute `harmonized' if `touse' `cond_state', ///
            			        max(`bracket_max') max_operator(`max_operator') ///
        						imput_flag(`harmonized'_f) flag_value(2)  cell(`i') `exclude_flags' conds(`conds') `list'
            			}
            			else {
            			    hotdeck_impute `harmonized' if `touse' `cond_state', ///
        						imput_flag(`harmonized'_f) flag_value(5)  cell(`i') `exclude_flags' conds(`conds') `list'
        				}
            		}
            		
            		if "`list'" != "" {
            		    di as result "Imputed cases:"
                		list `conds' `extralistvars' `varlist' `min' `max' `cell' `harmonized' `harmonized'_f if cell == `i' & `touse', noobs
                	}
            	}
            	drop cell
            }
        	qui count if inlist(`varlist',`exclude_values',.) & ((`harmonized' < `min' & !mi(`min')) | (`harmonized' > `max' & !mi(`max'))) & `touse'
        	if r(N) > 0 {
        		di
        		di as error "Imputed values are outside of the reported brackets in the following cases:"
        		list `conds' `extralistvars' `varlist' `min' `max' `harmonized' `harmonized'_f if ///
        				inlist(`varlist',`exclude_values',.) & ((`harmonized' < `min' & !mi(`min')) | (`harmonized' > `max' & !mi(`max'))) & `touse',noobs
        	}
        	drop group
        }
        else {
            qui count if `touse' & inw`w'==1 & mi(`harmonized')
            if `r(N)' > 0 {
                qui egen cell = group(`conds') if `touse' & inw`w'==1 & mi(`harmonized')
                
                su cell, meanonly
            	forvalues i = 1/`r(max)' {
            	    di
            	    local cond_state
            	    foreach var of local conds {
                	    qui sum `var' if cell == `i'
                	    if "`r(mean)'" != "" {
                	        local cmean = `r(mean)'
                	    }
                	    else {
                	        local cmean = .
                	    }
                	    di as result "`var'=`cmean'"
                	    local cond_state `cond_state' & `var' == `cmean'
                	}
                    
                    if "`dkown'" == "dkown" {
                        di
            		    di as result "Ownership unknown"
                        hotdeck_impute `harmonized' if `touse' `cond_state', ///
                						   imput_flag(`harmonized'_f) flag_value(7) cell(`i') `exclude_flags'  conds(`conds') `list'
                	}
                	else {
                	    di "well we got to here"
                	    hotdeck_impute `harmonized' if `touse' `cond_state', ///
                						   imput_flag(`harmonized'_f) flag_value(5) cell(`i') `exclude_flags'  conds(`conds') `list'
                	}
            	
            	    if "`list'" != "" {
            	        di as result "Imputed cases:"
                		list `conds' `extralistvars' `varlist' `cell' `harmonized' `harmonized'_f if cell == `i' & `touse', noobs
                	}
            	}
            	drop cell
            }
        }
    }
	
	
    
end

    
 
capture program drop hotdeck_impute
program define hotdeck_impute
syntax varname [if] [in], imput_flag(varname) flag_value(integer) cell(integer) ///
        [min(real 9999.999) max(real 9999.999) min_operator(string) max_operator(string) ///
        exclude_flags(string) conds(varlist) outlier(real 100) list extralistvars(varlist)]

	marksample touse, novarlist
	
	capture drop hot
	capture drop `harmonized'_i
	
	if `outlier' != 100 {
        _pctile `varlist', p(`outlier')
        local outlier_restric & `varlist' <= `r(r1)'
    }
	
	if `min' != 9999.999 {
	    *di as result "use min=`min'"
	    if `max' != 9999.999 {
	        *di as result "use max=`max'"
	        if `min' == `max' {
	            local hotcondition cell == `cell' | (`varlist' == `min' `outlier_restric')
	        }
	        else if `min' < `max' {
	            local hotcondition cell == `cell' | (`varlist' `min_operator' `min' & `varlist' `max_operator' `max' `outlier_restric')
	        }
	        else {
	            di as error "max (`max') is less than the min (`min')"
	            exit 198
	        }
	    }
	    else {
	        local hotcondition cell == `cell' | (`varlist' `min_operator' `min' & !mi(`varlist') `outlier_restric')
	    }
	}
	else {
	    if `max' != 9999.999 {
	        *di as result "use max=`max'"
	        local hotcondition cell == `cell' | (`varlist' `max_operator' `max' & !mi(`varlist') `outlier_restric')
	    }
	    else {
	        local hotcondition cell == `cell' | (!mi(`varlist') `outlier_restric')
	    }
	}
	
	if "`exclude_flags'" != "" {
	    local exclude_cond & !inlist(`imput_flag',`exclude_flags')
	}
	else {
	    local exclude_cond
	}
	
	qui gen hot = 1 if `touse' & (`hotcondition') `exclude_cond'
	
	qui count if `touse' & (`hotcondition') `exclude_cond' & !mi(`varlist')
	local num_not_miss = r(N)
	qui count if `touse' & (`hotcondition') `exclude_cond' & mi(`varlist')
	local num_miss = r(N)
	su `varlist' if hot == 1, meanonly
	if `r(N)' < 1 {
		di as res  "Number of observations without missing values:" `num_not_miss'
		di as res  "Number of observations with    missing values:" `num_miss' 
		di as error "Warning: No donors inside bracket."
		if `min' != 9999.999 {
    	    if `max' != 9999.999 {
    	        qui gen double `varlist'_i = (`max'-`min')/2 + `min' if hot == 1
    		    di as error "Imputing `varlist' as the midpoint between the upper and lower bracket"
    	    }
    	    else {
    	        qui gen double `varlist'_i = `min'*2 if hot == 1
    	        di as error "Imputing `varlist' as twice the lower bracket"
    	    }
    	}
    	else {
    	    if `max' != 9999.999 {
    	        qui gen double `varlist'_i = `max'/2 if hot == 1
    	        di as error "Imputing `varlist' as half the maximum bracket"
    	    }
    	    else {
    	        qui gen double `varlist'_i = .
    	        di as error "Imputing `varlist' as missing, no upper or lower bounds exist"
    	    }
    	}
    	if "`list'" == "list" {
    	    qui egen print = seq() if `touse' & (`hotcondition') `exclude_cond' & !mi(`varlist')
    	    di as result "Donors:"
    	    list `conds' `extralistvars' `varlist' `imput_flag' if print <= 20, noobs
    	    qui drop print
    	}
    	qui replace `imput_flag' = `flag_value' if `varlist' == . & `varlist'_i != .    
		replace `varlist' = `varlist'_i if `varlist' == . & `varlist'_i != .
		drop `varlist'_i
	}
	else if `num_miss' > 0 {
		hotdeckvar `varlist' if hot == 1
		if "`list'" == "list" {
		    qui egen print = seq() if `touse' & (`hotcondition') `exclude_cond' & !mi(`varlist')
		    di as result "Donors:"
		    list `conds' `extralistvars' `varlist' `imput_flag' if print <=20, noobs
		    qui drop print
		}
		qui replace `imput_flag' = `flag_value' if `varlist' == . & `varlist'_i != .
		qui replace `varlist' = `varlist'_i if `varlist' == . & `varlist'_i != .
		drop `varlist'_i
	}
	else {
		di as res  "No missing values for respondents in this cell"
	}
	drop hot
end

capture program drop parse_share_bracketvars
program define parse_share_bracketvars
syntax varname [if] [in], bv1_var(varname) bv2_var(varname) bv3_var(varname) ///
                          bounds_var(varname) lt_var(varname) mt_var(varname)
    marksample touse, novarlist 
    
    qui replace `lt_var' = `bv1_var' if `bounds_var' == 1 & `touse'
    qui replace `lt_var' = `bv1_var' if `bounds_var' == 2 & `touse'
    qui replace `lt_var' = `bv2_var' if `bounds_var' == 3 & `touse'
    qui replace `lt_var' = `bv2_var' if `bounds_var' == 4 & `touse'
    qui replace `lt_var' = `bv3_var' if `bounds_var' == 5 & `touse'
    qui replace `lt_var' = `bv3_var' if `bounds_var' == 6 & `touse'
    
    qui replace `mt_var' = `bv1_var' if `bounds_var' == 2 & `touse'
    qui replace `mt_var' = `bv1_var' if `bounds_var' == 3 & `touse'
    qui replace `mt_var' = `bv2_var' if `bounds_var' == 4 & `touse'
    qui replace `mt_var' = `bv2_var' if `bounds_var' == 5 & `touse'
    qui replace `mt_var' = `bv3_var' if `bounds_var' == 6 & `touse'
    qui replace `mt_var' = `bv3_var' if `bounds_var' == 7 & `touse'
    
end
    
capture program drop creles_entry_check
program define creles_entry_check
syntax varname [if] [in], entryvar(varname) [extralistvars(varlist)]
    marksample touse, novarlist 
    qui count if `entryvar' == 0 & !mi(`varlist') & `touse'
    if  `r(N)' > 0 {
        di
        di as error "Warning: `r(N)' respondents were given no ownership for `varlist' but have values"
        list `extralistvars' `entryvar' `varlist' if `entryvar' == 0 & !mi(`varlist'), noobs
    }
    
end
    
capture program drop imputation_summary_table
program define imputation_summary_table
syntax varname [if] [in], [min(varname) max(varname)  min_operator(string) max_operator(string)]
    marksample touse, novarlist 
    
    capture matrix drop B
    capture matrix drop C
    qui count if `varlist'_f == 1 & `touse'
    if `r(N)' > 0 {
        marg_distributions `varlist' if `varlist'_f == 1 & `touse', flag(`varlist'_f)
        matrix B = r(horizontal)
    }
    qui count if `varlist'_f == 6 & `touse'
    if `r(N)' > 0 {
        marg_distributions `varlist' if `varlist'_f == 6 & `touse', flag(`varlist'_f)
        matrix C = r(horizontal)
        local ownership yes 
    }
    else {
        local ownership no
    }
    marg_distributions `varlist' if inlist(`varlist'_f,1,6) & `touse', flag(`varlist'_f)
    matrix D = r(horizontal)
    
    
    capture confirm matrix B
    if !_rc {
        capture confirm matrix C
        if !_rc {
            matrix A = B[1...,8..8],B[1...,1..7] \ C[1...,8..8],C[1...,1..7] \ D[1...,8..8],D[1...,1..7]
            matrix rownames A = "CONTINUOUS" "NO OWNERSHIP" "All reported"
            matlist A, nodotz cspec(& %15s | %10.0fc & %10.1f & %10.1f & %10.2f & %10.1f & %10.1f & %10.1f & %10.1f &) rspec( & - & - - ) title("Reported Values")
        }
        else {
            matrix A = B[1...,8..8],B[1...,1..6]
            matrix rownames A = "CONTINUOUS"
            matlist A, nodotz cspec(& %15s | %10.0fc & %10.1f & %10.1f & %10.2f & %10.1f & %10.1f & %10.1f &) rspec( & - - ) title("Reported Values")
        }
    }
    else {
        capture confirm matrix C
        if !_rc {
            matrix A = C[1...,8..8],C[1...,1..7]
            matrix rownames A = "NO OWNERSHIP"
            matlist A, nodotz cspec(& %15s | %10.0fc & %10.1f & %10.1f & %10.2f & %10.1f & %10.1f & %10.1f & %10.1f &) rspec( & - - ) title("Reported Values")
        }
    }

    capture matrix drop B
    local rspec -
    qui count if `touse' & inlist(`varlist'_f,2,3)
    if `r(N)' > 0 {
        if "`min'" != "" | "`max'" != "" {
            tempvar cell
            qui count if `touse' & `varlist'_f == 2
            if `r(N)' > 0 {
                qui egen `cell' = group(`min' `max') if `touse' & `varlist'_f == 2, missing
                matrix B = [.z,.z,.z,.z,.z,.z,.z,.z]
                matrix rowname B = "CLOSED BRACKETS"
                local rspec `rspec' -
                su `cell', meanonly
            	forvalues i = 1 / `r(max)' {
            		su `min' if `cell' == `i', meanonly
            		local bracket_min = r(mean)
            		su `max' if `cell' == `i', meanonly    
            		local bracket_max = r(mean)
                    if "`bracket_max'" != "." {
            			if "`bracket_min'" == "." {
                            marg_distributions `varlist' if `cell' == `i' & `touse', flag(`varlist'_f)
                            matrix C = r(horizontal)
                            matrix rownames C = "Imputed values"
                            matrix roweq C = "0-`bracket_max'"
                            matrix B = B \ C
                            qui count if `varlist'_f == 1 & `varlist' `max_operator' `bracket_max' & `touse'
                            if `r(N)' > 0 {
                                marg_distributions `varlist' if `varlist'_f == 1 & `varlist' `max_operator' `bracket_max' & `touse', flag(`varlist'_f)
                                matrix C = r(horizontal)
                            }
                            else {
                                matrix C = [.,.,.,.,.,.,.,0]
                            }
                            matrix rownames C = "Donor values"
                            matrix roweq C = "0-`bracket_max'"
                            matrix B = B \ C
                            local rspec `rspec' & &
                        }
                    }
                }
                su `cell', meanonly
                forvalues i = 1 / `r(max)' {
            		su `min' if `cell' == `i', meanonly
            		local bracket_min = r(mean)
            		su `max' if `cell' == `i', meanonly    
            		local bracket_max = r(mean)
                    if "`bracket_max'" != "." {
            			if "`bracket_min'" != "." {
                            marg_distributions `varlist' if `cell' == `i' & `touse', flag(`varlist'_f)
                            matrix C = r(horizontal)
                            matrix rownames C = "Imputed values"
                            matrix roweq C = "`bracket_min'-`bracket_max'"
                            matrix B = B \ C
                            qui count if `varlist'_f == 1 & `varlist' `min_operator' `bracket_min' & `varlist' `max_operator' `bracket_max' & `touse'
                            if `r(N)' > 0 {
                                marg_distributions `varlist' if `varlist'_f == 1 & `varlist' `min_operator' `bracket_min' & `varlist' `max_operator' `bracket_max' & `touse', flag(`varlist'_f)
                                matrix C = r(horizontal)
                            }
                            else {
                                matrix C = [.,.,.,.,.,.,.,0]
                            }
                            matrix rownames C = "Donor values"
                            matrix roweq C = "`bracket_min'-`bracket_max'"
                            matrix B = B \ C
                            local rspec `rspec' & &
                        }
                    }
                }
            }
            qui count if `touse' & `varlist'_f == 3
            if `r(N)' > 0 {
                tempvar cell
                qui egen `cell' = group(`min' `max') if `touse' & `varlist'_f == 3, missing
                matrix C = [.z,.z,.z,.z,.z,.z,.z,.z]
                matrix rowname C = "OPEN BRACKETS"
                local rspec `rspec' -
                capture confirm matrix B
                if !_rc {
                    matrix B = B\C
                }
                else {
                    matrix B = C
                }
                su `cell', meanonly
            	forvalues i = 1/`r(max)' {
            		su `min' if `cell' == `i', meanonly
            		local bracket_min = r(mean)
                    marg_distributions `varlist' if `cell' == `i' & `touse', flag(`varlist'_f)
                    matrix C = r(horizontal)
                    matrix rownames C = "Imputed values"
                    matrix roweq C = "`bracket_min'+"
                    matrix B = B \ C
                    qui count if `varlist'_f == 1 & `varlist' `min_operator' `bracket_min' & `touse'
                    if `r(N)' > 0 {
                        marg_distributions `varlist' if `varlist'_f == 1 & `varlist' `min_operator' `bracket_min' & `touse', flag(`varlist'_f)
                        matrix C = r(horizontal)
                    }
                    else {
                        matrix C = [.,.,.,.,.,.,.,0]
                    }
                    matrix rownames C = "Donor values"
                    matrix roweq C = "`bracket_min'+"
                    matrix B = B \ C
                    local rspec `rspec' & &
                }
            }
        }
    }
    
    qui count if `varlist'_f == 5 & `touse'
    if `r(N)' > 0 {
        matrix C = [.z,.z,.z,.z,.z,.z,.z,.z]
        matrix rowname C = "NO BRACKET/VALUE"
        local rspec `rspec' -
        capture confirm matrix B
        if !_rc {
            matrix B = B\C
        }
        else {
            matrix B = C
        }
        marg_distributions `varlist' if `varlist'_f == 5 & `touse', flag(`varlist'_f)
        matrix C = r(horizontal)
        matrix rownames C = "Imputed values"
        matrix B = B \ C
        marg_distributions `varlist' if `varlist'_f == 1 & `touse', flag(`varlist'_f)
        matrix C = r(horizontal)
        matrix rownames C = "Donor values"
        matrix B = B \ C
        local rspec `rspec' & &
    }
    qui count if `varlist'_f == 7 & `touse'
    if `r(N)' > 0 {
        matrix C = [.z,.z,.z,.z,.z,.z,.z,.z]
        matrix rowname C = "DK"
        local rspec `rspec' -
        capture confirm matrix B
        if !_rc {
            matrix B = B\C
        }
        else {
            matrix B = C
        }
        marg_distributions `varlist' if `varlist'_f == 7 & `touse', flag(`varlist'_f)
        matrix C = r(horizontal)
        matrix rownames C = "Imputed values"
        matrix B = B \ C
        marg_distributions `varlist' if inlist(`varlist'_f,1,6) & `touse', flag(`varlist'_f)
        matrix C = r(horizontal)
        matrix rownames C = "Donor values"
        matrix B = B \ C
        local rspec `rspec' & &
    }
    
*    matrix C = [.z,.z,.z,.z,.z,.z,.z,.z]
*    matrix rowname C = "ALL"
*    local rspec `rspec' -
*    matrix B = B\C
*    marg_distributions `varlist' if inlist(`varlist'_f,2,3,5,7) & `touse', flag(`varlist'_f)
*    matrix C = r(horizontal)
*    matrix rownames C = "Imputed values"
*    matrix B = B \ C
*    marg_distributions `varlist' if inlist(`varlist'_f,1,6) & `touse', flag(`varlist'_f)
*    matrix C = r(horizontal)
*    matrix rownames C = "Donor values"
*    matrix B = B \ C
*    local rspec `rspec' & &
    
    capture confirm matrix B
    if !_rc {
        if "`ownership'" == "yes" {
            matrix A = B[1...,8..8],B[1...,1..7]
            matrix colnames A = n mean sd skewness median min max ownership
            matlist A, nodotz cspec(& %20s | %10.0fc & %10.1f & %10.1f & %10.2f & %10.1f & %10.1f & %10.1f & %10.1f &) rspec( `rspec' - ) title("Imputed Values")
        }
        else {
            matrix A = B[1...,8..8],B[1...,1..6]
            matrix colnames A = n mean sd skewness median min max
            matlist A, nodotz cspec(& %20s | %10.0fc & %10.1f & %10.1f & %10.2f & %10.1f & %10.1f & %10.1f &) rspec( `rspec' - ) title("Imputed Values")
        }
    }
end
    
capture program drop marg_distributions
program define marg_distributions, rclass
syntax varname [if] [in], flag(varname)
    marksample touse, novarlist
    
    qui sum `varlist' if `touse',d
    matrix A_ = [`r(mean)' \ `r(sd)' \ `r(skewness)' \ `r(p50)' \ `r(min)' \ `r(max)' ]
    
    qui count if `touse' & (`flag' == 6 | (inlist(`flag',7,9) & `varlist' == 0))
    local num `r(N)'
    qui count if `touse' & !mi(`varlist')
    local den `r(N)'
    local per = ((`den'-`num')/`den')*100
    matrix A_ = A_ \ [`per']
    matrix A_ = A_ \ [`den']
    
    matrix colnames A_ = "`varlist'" 
    matrix rownames A_ = mean sd skewness median min max ownership n
    *matrix list A_, noheader
    
    matrix B_ = A_'
    *matlist B_, nodotz noheader cspec(& %15s | %10.2fc & %10.2fc & %10.4f & %10.0f & %10.2f &) rspec( & - & )
    return matrix vertical = A_
    return matrix horizontal = B_
    
end

*count distinct observations
ssc install distinct


********************************************************************************************************************
********************************************************************************************************************

***load full set of SHARE respodents***
use mergeid using "$tracker"

********************************************************************************************************************
********************************************************************************************************************


*SHARE countries
label define share_countries ///
    11 "11.Austria" ///
    12 "12.Germany" ///
    13 "13.Sweden" ///
    14 "14.Netherlands" ///
    15 "15.Spain" ///
    16 "16.Italy" ///
    17 "17.France" ///
    18 "18.Denmark" ///
    19 "19.Greece" ///
    20 "20.Switzerland" ///
    23 "23.Belgium" ///
    25 "25.Israel" ///
    28 "28.Czechia" ///
    29 "29.Poland" ///
    30 "30.Ireland" ///
    31 "31.Luxembourg" ///
    32 "32.Hungary" ///
    33 "33.Portugal" ///
    34 "34.Slovenia" ///
    35 "35.Estonia"	///
	47 "47.Croatia"

*whether in wave
label define inw ///
   0 "0.nonresp" ///
   1 "1.resp,alive" 
   
*interview status
label define wstat ///
   0 "0.inap." ///
   1 "1.resp, alive"  ///
   4 "4.nr, alive" ///
   5 "5.nr, died this wv" ///
   6 "6.nr, died prev wv" ///
   7 "7.nr, dropped from samp" ///
   9 "9.nr, dk if alive or died"
   
*cohort
label define cohort  ///
    1 "1.Original sample for country" ///
    2 "2.2006 Refreshment sample" ///
    3 "3.2010 Refreshment sample" ///
    4 "4.2012 Refreshment sample" ///
	5."5.2014 Refreshment sample"

*wave in which sampled
label define wavsamp ///
  1 "1.SHARE w1 (2004-2005)" ///
  2 "2.SHARE w2 (2006-2007)" ///
  4 "4.SHARE w4 (2010-2012)" ///
  5 "5.SHARE w5 (2013)" ///
  6 "6.SHARE w6 (2015)"
  
*household sample type
label define hsamp ///
  1 "1.main sample" ///
  2 "2.vignette sample" 
  
*whether couple household
label define cpl ///
   0 "0.not coupled" ///
   1 "1.coupled" 
   
*financial respondent indicator
label define finr ///
   0 "0.no" ///
   1 "1.yes" 
   
*household respondent indicator
label define hhr ///
   0 "0.no" ///
   1 "1.yes" 
   
*family respondent indicator
label define famr ///
   0 "0.no" ///
   1 "1.yes" 
   
*proxy indicator
label define proxy ///
   0 "0.no proxy" ///
   1 "1.some proxy" ///
   2 "2.all proxy" ///
   .d ".d:DK" ///
   .m ".m:missing" ///
   .r ".r:refuse"
   
*interview date flag
label define dateflag ///
   0 "0.m/y ok" ///
   1 "1.month miss"  ///
   2 "2.mon/yr miss" ///
   .d ".d:DK" ///
   .m ".m:missing" ///
   .r ".r:refuse"
   
*death date indicator
label define deathd ///
	.x ".x:Alive"
   
*sampling eligibility
label define share_status ///
   0 "0.not in w1 sample/not age-elig" ///
   1 "1.w1 age-eligible sample" 
   
*gender
label define gender ///
   1 "1.male"  ///
   2 "2.female" , replace
   
*gender flag   
label define genderflag ///
   0 "0.no gender problem" ///
   1 "1.gender prob, used first"
   
*education flag
label define educyrs ///
   0 "0.none" ///
   .i ".i:implausible" ///
   .o ".o:other" ///
   .n ".n:not yet coded" ///
   .d ".d:DK" ///
   .m ".m:missing" ///
   .r ".r:refuse"

*education in years flag   
label define edyrsf ///
	1 "1.edu years imputed from isced code (w1)" ///
	2 "2.edu years surveyed (w2 forward)"
	
*harmonized education
label define edharm ///
	1 "1.less than upper secondary education" ///
	2 "2.upper secondary and vocational training" ///
	3 "3.tertiary education"
   
*education by ISCED code
label define edisced ///
   0 "0.None" ///
   1 "1.Primary education" ///
   2 "2.Lower secondary education" ///
   3 "3.Upper secondary education" ///
   4 "4.Post-secondary non tertiary education" ///
   5 "5.First stage of tertiary education" ///
   6 "6.Second stage of tertiary education" ///
   .o ".o=other" ///
   .s ".s=still in school" ///
   .n ".n=none" ///
   .m ".m=missing"
   
*education by ISCED code flag
label define i_flag ///
   0 "0.not imputed" ///
   1 "1.imputed" ///
   .m ".m:missing"
   
*marital status with partnership
label define marwpart ///
   1 "1.married"  ///
   3 "3.partnered" ///
   4 "4.separated" ///
   5 "5.divorced" ///
   7 "7.widowed" ///
   8 "8.never married" ///
   .d ".d:DK" ///
   .m ".m:missing" ///
   .r ".r:refuse"
   
*implied partnership status
label define part ///
   0 "0.no"  ///
   1 "1.yes" ///
   .d ".d:DK" ///
   .m ".m:missing" ///
   .r ".r:refuse"
   
*marital status without implied partnership
label define marwopart ///
   1 "1.married"  ///
   3 "3.registered partnership" ///
   4 "4.separated" ///
   5 "5.divorced" ///
   7 "7.widowed" ///
   8 "8.never married" ///
   .d ".d:DK" ///
   .m ".m:missing" ///
   .r ".r:refuse"
   
*value religion
label define relig ///
   1 "1.Protestant"  ///
   2 "2.Catholic" ///
   3 "3.Jewish" ///
   4 "4.None/no pref" ///
   5 "5.Other" ///
   7 "7.Muslim" ///
   8 "8.Orthodox"
   
*countries
label define countries ///
	.n ".n:not yet coded" ///
	002 "002.Africa" ///
	004 "004.Afghanistan" ///
	005 "005.South America" ///
	008 "008.Albania" ///
	010 "010.Antarctica" ///
	248 "248.?land Islands" ///
	008 "008.Albania" ///
	012 "012.Algeria" ///
	016 "016.American Samoa" ///
	020 "020.Andorra" ///
	024 "024.Angola" ///
	660 "660.Anguilla" ///
	028 "028.Antigua and Barbuda" ///
	032 "032.Argentina" ///
	051 "051.Armenia" ///
	533 "533.Aruba" ///
	036 "036.Australia" ///
	040 "040.Austria" ///
	031 "031.Azerbaijan" ///
	044 "044.Bahamas" ///
	048 "048.Bahrain" ///
	050 "050.Bangladesh" ///
	052 "052.Barbados" ///
	112 "112.Belarus" ///
	056 "056.Belgium" ///
	084 "084.Belize" ///
	204 "204.Benin" ///
	060 "060.Bermuda" ///
	064 "064.Bhutan" ///
	068 "068.Bolivia (Plurinational State of)" ///
	535 "535.Bonaire, Saint Eustatius and Saba" ///
	070 "070.Bosnia and Herzegovina" ///
	072 "072.Botswana" ///
	076 "076.Brazil" ///
	092 "092.British Virgin Islands" ///
	096 "096.Brunei Darussalam" ///
	100 "100.Bulgaria" ///
	854 "854.Burkina Faso" ///
	108 "108.Burundi" ///
	116 "116.Cambodia" ///
	120 "120.Cameroon" ///
	124 "124.Canada" ///
	132 "132.Cape Verde" ///
	136 "136.Cayman Islands" ///
	140 "140.Central African Republic" ///
	148 "148.Chad" ///
	830 "830.Channel Islands" ///
	152 "152.Chile" ///
	156 "156.China" ///
	344 "344.China, Hong Kong Special Administrative Region" ///
	446 "446.China, Macao Special Administrative Region" ///
	170 "170.Colombia" ///
	174 "174.Comoros" ///
	178 "178.Congo" ///
	184 "184.Cook Islands" ///
	188 "188.Costa Rica" ///
	384 "384.C?te d'Ivoire" ///
	191 "191.Croatia" ///
	192 "192.Cuba" ///
	531 "531.Cura?ao" ///
	196 "196.Cyprus" ///
	203 "203.Czech Republic" ///
	200 "200.Czechoslovakia" ///
	408 "408.Democratic People's Republic of Korea" ///
	180 "180.Democratic Republic of the Congo" ///
	208 "208.Denmark" ///
	262 "262.Djibouti" ///
	212 "212.Dominica" ///
	214 "214.Dominican Republic" ///
	218 "218.Ecuador" ///
	818 "818.Egypt" ///
	222 "222.El Salvador" ///
	226 "226.Equatorial Guinea" ///
	232 "232.Eritrea" ///
	233 "233.Estonia" ///
	230 "230.Ethiopia (before Eritrea broke away)" ///
	231 "231.Ethiopia" ///
	234 "234.Faeroe Islands" ///
	238 "238.Falkland Islands (Malvinas)" ///
	242 "242.Fiji" ///
	246 "246.Finland" ///
	250 "250.France" ///
	254 "254.French Guiana" ///
	258 "258.French Polynesia" ///
	266 "266.Gabon" ///
	270 "270.Gambia" ///
	268 "268.Georgia" ///
	276 "276.Germany" ///
	288 "288.Ghana" ///
	292 "292.Gibraltar" ///
	300 "300.Greece" ///
	304 "304.Greenland" ///
	308 "308.Grenada" ///
	312 "312.Guadeloupe" ///
	316 "316.Guam" ///
	320 "320.Guatemala" ///
	831 "831.Guernsey" ///
	324 "324.Guinea" ///
	624 "624.Guinea-Bissau" ///
	328 "328.Guyana" ///
	332 "332.Haiti" ///
	336 "336.Holy See" ///
	340 "340.Honduras" ///
	348 "348.Hungary" ///
	352 "352.Iceland" ///
	356 "356.India" ///
	360 "360.Indonesia" ///
	364 "364.Iran (Islamic Republic of)" ///
	368 "368.Iraq" ///
	372 "372.Ireland" ///
	833 "833.Isle of Man" ///
	376 "376.Israel" ///
	380 "380.Italy" ///
	388 "388.Jamaica" ///
	392 "392.Japan" ///
	832 "832.Jersey" ///
	400 "400.Jordan" ///
	398 "398.Kazakhstan" ///
	404 "404.Kenya" ///
	296 "296.Kiribati" ///
	414 "414.Kuwait" ///
	417 "417.Kyrgyzstan" ///
	418 "418.Lao People Democratic Republic" ///
	428 "428.Latvia" ///
	422 "422.Lebanon" ///
	426 "426.Lesotho" ///
	430 "430.Liberia" ///
	434 "434.Libyan Arab Jamahiriya" ///
	438 "438.Liechtenstein" ///
	440 "440.Lithuania" ///
	442 "442.Luxembourg" ///
	450 "450.Madagascar" ///
	454 "454.Malawi" ///
	458 "458.Malaysia" ///
	462 "462.Maldives" ///
	466 "466.Mali" ///
	470 "470.Malta" ///
	584 "584.Marshall Islands" ///
	474 "474.Martinique" ///
	478 "478.Mauritania" ///
	480 "480.Mauritius" ///
	175 "175.Mayotte" ///
	484 "484.Mexico" ///
	583 "583.Micronesia (Federated States of)" ///
	492 "492.Monaco" ///
	496 "496.Mongolia" ///
	499 "499.Montenegro" ///
	500 "500.Montserrat" ///
	504 "504.Morocco" ///
	508 "508.Mozambique" ///
	104 "104.Myanmar" ///
	516 "516.Namibia" ///
	520 "520.Nauru" ///
	524 "524.Nepal" ///
	528 "528.Netherlands" ///
	530 "530.Netherlands Antilles" ///
	532 "532.Netherlands Antilles (before Aruba broke away)" ///
	540 "540.New Caledonia" ///
	554 "554.New Zealand" ///
	558 "558.Nicaragua" ///
	562 "562.Niger" ///
	566 "566.Nigeria" ///
	570 "570.Niue" ///
	574 "574.Norfolk Island" ///
	580 "580.Northern Mariana Islands" ///
	578 "578.Norway" ///
	275 "275.Occupied Palestinian Territory" ///
	512 "512.Oman" ///
	586 "586.Pakistan" ///
	585 "585.Palau" ///
	591 "591.Panama" ///
	598 "598.Papua New Guinea" ///
	600 "600.Paraguay" ///
	604 "604.Peru" ///
	608 "608.Philippines" ///
	612 "612.Pitcairn" ///
	616 "616.Poland" ///
	620 "620.Portugal" ///
	630 "630.Puerto Rico" ///
	634 "634.Qatar" ///
	410 "410.Republic of Korea" ///
	498 "498.Republic of Moldova" ///
	638 "638.R?union" ///
	642 "642.Romania" ///
	643 "643.Russian Federation" ///
	646 "646.Rwanda" ///
	652 "652.Saint-Barth?lemy" ///
	654 "654.Saint Helena" ///
	659 "659.Saint Kitts and Nevis" ///
	662 "662.Saint Lucia" ///
	663 "663.Saint-Martin (French part)	" ///
	666 "666.Saint Pierre and Miquelon" ///
	670 "670.Saint Vincent and the Grenadines" ///
	882 "882.Samoa" ///
	674 "674.San Marino" ///
	678 "678.Sao Tome and Principe" ///
	682 "682.Saudi Arabia" ///
	686 "686.Senegal" ///
	688 "688.Serbia" ///
	690 "690.Seychelles" ///
	694 "694.Sierra Leone" ///
	702 "702.Singapore" ///
	534 "534.Sint Maarten (Dutch part)	" ///
	703 "703.Slovakia" ///
	705 "705.Slovenia" ///
	090 "090.Solomon Islands" ///
	706 "706.Somalia" ///
	710 "710.South Africa" ///
	724 "724.Spain" ///
	144 "144.Sri Lanka" ///
	736 "736.Sudan" ///
	740 "740.Suriname" ///
	744 "744.Svalbard and Jan Mayen Islands" ///
	748 "748.Swaziland" ///
	752 "752.Sweden" ///
	756 "756.Switzerland" ///
	760 "760.Syrian Arab Republic" ///
	762 "762.Tajikistan" ///
	764 "764.Thailand" ///
	807 "807.The former Yugoslav Republic of Macedonia" ///
	626 "626.Timor-Leste" ///
	768 "768.Togo" ///
	772 "772.Tokelau" ///
	776 "776.Tonga" ///
	780 "780.Trinidad and Tobago" ///
	788 "788.Tunisia" ///
	792 "792.Turkey" ///
	795 "795.Turkmenistan" ///
	796 "796.Turks and Caicos Islands" ///
	798 "798.Tuvalu" ///
	800 "800.Uganda" ///
	804 "804.Ukraine" ///
	810 "810.U.S.S.R." ///
	784 "784.United Arab Emirates" ///
	826 "826.United Kingdom of Great Britain and Northern Ireland" ///
	834 "834.United Republic of Tanzania" ///
	840 "840.United States of America" ///
	850 "850.United States Virgin Islands" ///
	858 "858.Uruguay" ///
	860 "860.Uzbekistan" ///
	548 "548.Vanuatu" ///
	862 "862.Venezuela (Bolivarian Republic of)" ///
	704 "704.Viet Nam" ///
	876 "876.Wallis and Futuna Islands" ///
	732 "732.Western Sahara" ///
	887 "887.Yemen" ///
	891 "891.Serbia and Montenegro" ///
	894 "894.Zambia" ///
	716 "716.Zimbabwe" ///
	890 "890.Socialist Federal Republic of Yugoslavia" ///
  1010 "1010.Congo (both)" ///
  1011 "1011.Stateless" ///
  1012 "1012.Cypriote-American" ///
  1015 "1015.EU-Citizenship" ///
  1016 "1016.Argentinean-Italian" ///
  1017 "1017.Serbian-Bosnian" ///
  1018 "1018.Austrian-Italian-Czech" ///
  1019 "1019.American-Irish)" ///
  1020 "1020.Galicia (Central Europe)" ///
  1021 "1021.Italian-Croatian" ///
  1022 "1022.Italian-Slovenian" ///
  1023 "1023.Portuguese-Swiss" ///
  1024 "1024.Afghan-Turkish" ///
  1025 "1025.Turkish-Kurdish" ///
  1026 "1026.Italian-Austrian" ///
  1027 "1027.German-Italian" ///
  1028 "1028.British-Estonian" ///
  1029 "1029.Dutch-Czech" ///
  1030 "1030.Former Territories of German Reich" ///
  1031 "1031.Former Eastern Terr. of German Reich" ///
  1040 "1040.Kosovo" ///
  1050 "1050.Minor Asia" ///
  1060 "1060.Former Netherlands East-Indies" ///
  1070 "1070.Former Austria-Hungary" ///
  1080 "1080.Kurdistan (Region)" ///
  1090 "1090.Borneo Island" ///
  1095 "1095.Former Protectorate of Northern Rhodesia" ///
  1100 "1100.Chechnya" ///
  1101 "German-Spanish" ///
  1103 "Caucasus" ///
  1110 "Tunisian-French" ///
  1130 "French-German" ///
  1131 "Italian-Uruguayan" ///
  1132 "Mexican-Swiss" ///
  .d ".d:DK" ///
  .m ".m:missing" ///
  .r ".r:refuse"
  
*born in interview country
label define bornin ///
	0 "0.out of country" ///
	1 "1.in country"
	
*language of interview
label define languages ///
	1 "1.German" ///
	2 "2.Swedish" ///
	3 "3.Dutch" ///
	4 "4.Spanish" ///
	5 "5.Italian" ///
	6 "6.French" ///
	7 "7.Danish" ///
	8 "8.Greek" ///
	9 "9.Flemish" ///
	10 "10.Hebrew" ///
	11 "11.Arabic" ///
	12 "12.Russian" ///
	13 "13.Czech" ///
	14 "14.Polish" ///
	15 "15.English" ///
	16 "16.Hungarian" ///
	17 "17.Portuguese" ///
	18 "18.Slovenian" ///
	19 "19.Estonian" ///
	20 "20.Catalan" ///
	21 "21.Croatian" ///
	22 "22.Luxembourgian,French,German,Portuguese" ///
	23 "23.Estonian or Russian" ///
	24 "24.Spanish or Catalan" ///
	25 "25.French or German" ///
	26 "26.French, German, or Portuguese" 
  
*urban or rural
label define rural ///
	0 "0.urban" ///
	1 "1.rural"
 
   
******************************************************************************************


*set wave number
local wv = 1

***merge with cover screen tracker (respondent) data***
local demo_all_cv_r mergeidp1 interview_w1 int_year_w1 int_month_w1 interview_hh_w1 ///
                fam_resp_w1 fin_resp_w1 hou_resp_w1 partnerinhh_w1 coupleid1 ///
                 gender yrbirth mobirth country waveid_hh firstwave_hh hhsize_w1 agep2004  
merge 1:1 mergeid using "$tracker", keepusing(`demo_all_cv_r' hhid1 age_int_w1 ) nogen

***merge with wave 1 Asset data***
local demo_w1_as as057_
merge 1:1 mergeid using "$wave_1_as", keepusing(`demo_w1_as') nogen

***merge with wave 1 Behavioural Risk data***
local demo_w1_br br017_
merge 1:1 mergeid using "$wave_1_br", keepusing(`demo_w1_br') nogen

***merge with wave 1 Children data***
local demo_w1_ch ch023_
merge 1:1 mergeid using "$wave_1_ch", keepusing(`demo_w1_ch') nogen

***merge with wave 1 Consumption data***
local demo_w1_co co009_
merge 1:1 mergeid using "$wave_1_co", keepusing(`demo_w1_co') nogen

***merge with wave 1 Demographic data***
local demo_w1_dn dn038_ dn014_ dn015_ dn017_ dn004_ dn005c dn002_ language dn042_
merge 1:1 mergeid using "$wave_1_dn", keepusing(`demo_w1_dn') nogen

***merge with wave 1 Employment and Pension data***
local demo_w1_ep ep210_
merge 1:1 mergeid using "$wave_1_ep", keepusing(`demo_w1_ep') nogen

***merge with wave 1 Financial Transfer data***
local demo_w1_ft ft021_
merge 1:1 mergeid using "$wave_1_ft", keepusing(`demo_w1_ft') nogen

***merge with wave 1 Health Care data***
local demo_w1_hc hc063_
merge 1:1 mergeid using "$wave_1_hc", keepusing(`demo_w1_hc') nogen

***merge with wave 1 Household Income data***
local demo_w1_hh hh014_
merge 1:1 mergeid using "$wave_1_hh", keepusing(`demo_w1_hh') nogen

***merge with wave 1 Housing data***
local demo_w1_ho ho041_ 
merge 1:1 mergeid using "$wave_1_ho", keepusing(`demo_w1_ho') nogen

***merge with wave 1 Interviewer data***
local demo_w1_iv iv009_ 
merge 1:1 mergeid using "$wave_1_iv", keepusing(`demo_w1_iv') nogen

***merge with wave 1 Physical Health data***
local demo_w1_ph ph054_
merge 1:1 mergeid using "$wave_1_ph", keepusing(`demo_w1_ph') nogen

***merge with wave 1 Social Support data***
local demo_w1_sp sp022_
merge 1:1 mergeid using "$wave_1_sp", keepusing(`demo_w1_sp') nogen

***merge with wave 1 Generated Variables (Weights) data***
local demo_w1_gv_weights cchw_w1 cciw_w1 dw_w1 ///
			             psu ssu stratum1 stratum2
merge 1:1 mergeid using "$wave_1_gv_weights", keepusing(`demo_w1_gv_weights') nogen

***merge with wave 1 Dropoff data***
local demo_w1_dropoff q1 q34_re 
merge 1:1 mergeid using "$wave_1_dropoff", keepusing(`demo_w1_dropoff') nogen

***merge with wave 1 Generated Variables (Imputations) data***
local demo_w1_imputations isced isced_f yedu yedu_f implicat p_nrp
merge 1:1 mergeid using "$wave_1_imputations_", keepusing(`demo_w1_imputations') nogen

***merge with wave 1 Generated Variables ISCED data***
local demo_w1_isced isced1997_r
merge 1:1 mergeid using "$wave_1_gv_isced", keepusing(`demo_w1_isced') nogen


***wave status: response indicator***
*wave 1 response indicator
gen inw1 = 0
replace inw1 = 1 if interview_w1 == 1
label variable inw1 "inw1: =1 if respondent w1"
label values inw1 inw

***wave status: drop-off response indicator***
*wave 1 response indicator
gen inw1sc = 0
replace inw1sc = 1 if !mi(q1)
label variable inw1sc "inw1sc: =1 if respondent w1 drop-off"
label values inw1sc inw

***person specific identifier***
*country id (char)
gen cid =""
replace cid = substr(mergeid,1,2)
replace cid = "11" if cid == "AT"
replace cid = "12" if cid == "DE"
replace cid = "13" if cid == "SE"
replace cid = "14" if cid == "NL"
replace cid = "15" if cid == "ES" | cid == "Eg"
replace cid = "16" if cid == "IT"
replace cid = "17" if cid == "FR" | cid == "F1"
replace cid = "18" if cid == "DK"
replace cid = "19" if cid == "GR"
replace cid = "20" if cid == "Cg"
replace cid = "21" if cid == "Cf"
replace cid = "22" if cid == "Ci"
replace cid = "23" if cid == "Bf"
replace cid = "24" if cid == "Bn"
replace cid = "25" if cid == "Ih"
replace cid = "26" if cid == "Ia"
replace cid = "27" if cid == "Ir"
replace cid = "28" if cid == "CZ"
replace cid = "29" if cid == "PL"
replace cid = "30" if cid == "IE"
replace cid = "31" if cid == "LU"
replace cid = "32" if cid == "HU"
replace cid = "33" if cid == "PT"
replace cid = "34" if cid == "SI"
replace cid = "35" if cid == "EE"
replace cid = "36" if cid == "HR"
label variable cid "cid: country id /2-char"

*household id (char)
gen hhidc=""
replace hhidc = substr(mergeid,4,6)
label variable hhidc "hhid: household id /6-char"

*person number (char)
gen pn =""
replace pn = substr(mergeid,11,2)
label variable pn "pn: Person number within household (char)"

*merge identifier (char)
label variable mergeid "mergeid: cid - hhid - pn /12-char"

*merge identifier (num)
*note: pn = a5, a6, b5, b6 are coverscreen respondents that outside of the household
*we convert them to numbers to create a numerical version of nmergeid
replace pn = "90" if pn == "a5"
replace pn = "91" if pn == "a6"
replace pn = "92" if pn == "b5"
replace pn = "93" if pn == "b6"

gen nmergeid =cid+hhidc+pn
destring nmergeid, replace
label variable nmergeid "nmergeid: cid + hhid + pn /num"
format nmergeid %12.0f

***household identifier***
*wave 1 household id (char)
tempvar subhh hhidc
gen `subhh' = substr(hhid1,11,1)
replace `subhh' = "1" if `subhh' == "A"
gen `hhidc' = substr(hhid1,4,6)
gen hh1hhidc =""
replace hh1hhidc = cid + `hhidc' + `subhh' if inw1==1
label variable hh1hhidc "hh1hhidc:w1 cid + hhold id + subhousehold /9-char"

*wave 1 household id (num)
destring hh1hhidc, generate(hh1hhid)
label variable hh1hhid "hh1hhid:w1 cid + hhold id + subhousehold /Num"
format hh1hhid %9.0f

label variable hhid1 "hhid1: SHARE household identifier wave 1"

***Country***
gen racountry = country

gen isocountry = .
replace isocountry = 040 if country == 11
replace isocountry = 276 if country == 12
replace isocountry = 752 if country == 13
replace isocountry = 528 if country == 14
replace isocountry = 724 if country == 15
replace isocountry = 380 if country == 16
replace isocountry = 250 if country == 17
replace isocountry = 208 if country == 18
replace isocountry = 300 if country == 19
replace isocountry = 756 if country == 20
replace isocountry = 056 if country == 23
replace isocountry = 376 if country == 25
replace isocountry = 203 if country == 28
replace isocountry = 616 if country == 29
replace isocountry = 372 if country == 30
replace isocountry = 442 if country == 31
replace isocountry = 348 if country == 32
replace isocountry = 620 if country == 33
replace isocountry = 705 if inlist(country, 34, 43)
replace isocountry = 233 if country == 35
replace isocountry = 191 if country == 47
label variable isocountry "UN numerical country code"
label values isocountry countries



***Implicate***
gen implicate = implicat

***couple identifier***
*wave 1 couple id (number)
gen pnp = substr(mergeidp1,11,2)

gen w1_hh_coup=""
levelsof pn if !mi(hhid1), local(pns)
foreach r of local pns {
	forvalues i = 1/9 {
		bysort hhid1: replace w1_hh_coup = "`r'"+"`i'" ///
			if pn == pnp[_n-`i'] & pn == "`r'"
		bysort hhid1: replace w1_hh_coup ="`r'"+"`i'" ///
			if w1_hh_coup[_n+`i'] == "`r'"+"`i'"
	}
}
replace w1_hh_coup=string(_n) if missing(w1_hh_coup)
egen h1coupid = group(hhid1 w1_hh_coup) if !mi(hhid1)
label variable h1coupid "wave 1 specific couple id number /num"

drop w1_hh_coup pnp

***Spouse Identifier***
*wave 1 spouse id (char)
gen s1mergeid = "0" if inw1 == 1
bysort h1coupid: replace s1mergeid = mergeid[_n+1] if !missing(mergeid[_n+1]) & !missing(h1coupid)
bysort h1coupid: replace s1mergeid = mergeid[_n-1] if !missing(mergeid[_n-1]) & !missing(h1coupid)
label variable s1mergeid "s1mergeid:w1 spouse mergeid /12-char"

*wave 1 spouse id (num)
gen s1nmergeid =""
replace s1nmergeid = "0" if s1mergeid == "0"

tempvar s1_cid s1_hhidc s1_pn

gen `s1_cid' = substr(s1mergeid,1,2)
replace `s1_cid' = "11" if `s1_cid' == "AT"
replace `s1_cid' = "12" if `s1_cid' == "DE"
replace `s1_cid' = "13" if `s1_cid' == "SE"
replace `s1_cid' = "14" if `s1_cid' == "NL"
replace `s1_cid' = "15" if `s1_cid' == "ES" | `s1_cid' == "Eg"
replace `s1_cid' = "16" if `s1_cid' == "IT"
replace `s1_cid' = "17" if `s1_cid' == "FR" | `s1_cid' == "F1"
replace `s1_cid' = "18" if `s1_cid' == "DK"
replace `s1_cid' = "19" if `s1_cid' == "GR"
replace `s1_cid' = "20" if `s1_cid' == "Cg"
replace `s1_cid' = "21" if `s1_cid' == "Cf"
replace `s1_cid' = "22" if `s1_cid' == "Ci"
replace `s1_cid' = "23" if `s1_cid' == "Bf"
replace `s1_cid' = "24" if `s1_cid' == "Bn"
replace `s1_cid' = "25" if `s1_cid' == "Ih"
replace `s1_cid' = "26" if `s1_cid' == "Ia"
replace `s1_cid' = "27" if `s1_cid' == "Ir"
replace `s1_cid' = "28" if `s1_cid' == "CZ"
replace `s1_cid' = "29" if `s1_cid' == "PL"
replace `s1_cid' = "30" if `s1_cid' == "IE"
replace `s1_cid' = "31" if `s1_cid' == "LU"

gen `s1_hhidc' = substr(s1mergeid,4,6)

gen `s1_pn' = substr(s1mergeid,11,2)

replace s1nmergeid =`s1_cid'+`s1_hhidc'+`s1_pn' if missing(s1nmergeid)
label variable s1nmergeid "s1nmergeid:w1 spouse mergeid /num"	// ?? supposed to be numerical? 
codebook s1nmergeid

*respondent number of spouse id's
gen raspct =.
replace raspct = 0 if s1nmergeid == "0"
replace raspct = 1 if !inlist(s1nmergeid,"0","")
label variable raspct "raspct: # of spouses with mergeid"

*respondent first spouse id
gen raspid1 =""
replace raspid1 = s1nmergeid if !inlist(s1nmergeid,"0","")
label variable raspid1 "raspid1: mergeid of 1st spouse"

*create temporary indicator of wave 1 spouse
gen w1spouse=0 if missing(mergeidp1) & inw1 == 1
bysort h1coupid: replace w1spouse = 1 if !mi(mergeidp1) & inw1[_n-1] == 1 & inw1 == 1 & !mi(h1coupid)
bysort h1coupid: replace w1spouse = 1 if !mi(mergeidp1) & inw1[_n+1] == 1 & inw1 == 1 & !mi(h1coupid)

bysort h1coupid: replace w1spouse = 2 if !mi(mergeidp1) & inw1[_n-1] == 0 & inw1 == 1 & !mi(h1coupid)
bysort h1coupid: replace w1spouse = 2 if !mi(mergeidp1) & inw1[_n+1] == 0 & inw1 == 1 & !mi(h1coupid)

***wave status: interview status***
*wave 1 interview status
gen r1iwstat =.
replace r1iwstat = 0
replace r1iwstat = 1 if inw1 == 1
label variable r1iwstat "r1iwstat:w1 r interview status"
label values r1iwstat wstat

*wave 1 spouse interview status
gen s1iwstat =.
spouse r1iwstat, result(s1iwstat) wave(1)
label variable s1iwstat "s1iwstat:w1 s interview status"
label values s1iwstat wstat

***cohort***
*sample cohort
gen hacohort = .
replace hacohort = 1 if inlist(waveid_hh,42,52)
label variable hacohort "hacohort: Sample cohort"
label values hacohort cohort

****Wave in which the household was sampled***
*household wave in which sampled
gen hawvsamp=.
replace hawvsamp = firstwave_hh
label variable hawvsamp "wave in which household was sampled"
label values hawvsamp wavsamp

***sampling weight***
*sampling weight, combined sample
gen r`wv'wtsamp =.
replace r`wv'wtsamp = dw_w1
label variable r`wv'wtsamp "r`wv'wtsamp:w`wv' design weight"

*respondent psu
gen rapsu1 =.
replace rapsu1 = real(psu)
label variable rapsu1 "rapsu1: primary sampling unit"

*respondent psu 2
gen rapsu2 =.
replace rapsu2 = real(ssu)
label variable rapsu2 "rapsu2: secondary sampling unit"

*respondent stratification variable
gen raestrat1 = stratum1
label variable raestrat1 "raestrat1: first stratification variable"

*respondent stratification variable 2
gen raestrat2 = stratum2
label variable raestrat2 "raestrat2: second stratification variable"

***Household analysis weight***
*wave 1 household analysis weight, combined sample
gen r`wv'wthh =.
replace r`wv'wthh = cchw_w1
label variable r`wv'wthh "r`wv'wthh:w`wv' household analysis weight"

***person-level analysis weight***
*wave 1 respondent analysis weight, combined sample
gen r`wv'wtresp =.
replace r`wv'wtresp = cciw_w1
label variable r`wv'wtresp "r`wv'wtresp:w`wv' person-level analysis weight"

***number of household respondents***     
*wave 1 household number of respondents 
egen hh1hhresp = sum(inw1==1) if inw1==1, by(hh1hhid)
label variable hh1hhresp "hh1hhresp:w1 # core respondents in hh"

***whether coupled***
*wave 1 couple indicator
egen h1cpl = sum(inw1==1), by(h1coupid)
recode h1cpl (0=.) (1=0) (2=1)
label variable h1cpl "h1cpl:w1 whether coupled"
label values h1cpl cpl

***financial respondent***
*wave 1 respondent financial respondent indicator
gen r`wv'finr =.
replace r`wv'finr = 0 if fin_resp_w1 == 0
replace r`wv'finr = 1 if fin_resp_w1 == 1
label variable r`wv'finr "r`wv'finr:w`wv' whether r financial resp"
label values r`wv'finr finr

*wave 1 spouse financial respondent indicator
gen s`wv'finr =.
spouse r`wv'finr, result(s`wv'finr) wave(`wv')
label variable s`wv'finr "s`wv'finr:w`wv' whether s financial resp"
label values s`wv'finr  finr

***hosuehold respondent***
*wave 1 respondent household respondent indicator
gen r`wv'hhr =.
replace r`wv'hhr = 1 if hou_resp_w1 == 1
replace r`wv'hhr = 0 if hou_resp_w1 == 0
label variable r`wv'hhr "r`wv'hhr:w`wv' whether r hh resp"
label values r`wv'hhr hhr

*wave 1 spouse household respondent indicator
gen s`wv'hhr =.
spouse r`wv'hhr, result(s`wv'hhr) wave(`wv')
label variable s`wv'hhr "s`wv'hhr:w`wv' whether s hh resp"
label values s`wv'hhr hhr

***family respondent***
*wave 1 respondent family respondent indicator
gen r`wv'famr =.
replace r`wv'famr = 1 if fam_resp_w1 == 1
replace r`wv'famr = 0 if fam_resp_w1 == 0
label variable r`wv'famr "r`wv'famr:w`wv' whether r family resp"
label values r`wv'famr   famr

*wave 1 spouse family respondent indicator
gen s`wv'famr =.
spouse r`wv'famr, result(s`wv'famr) wave(`wv')
label variable s`wv'famr "s`wv'famr:w`wv' whether s family resp"
label values s`wv'famr  famr

***whether proxy interview***
*wave 1 respondent proxy indicator
gen r1proxy =.
missing_common dn038 ph054 br017 hc063 ep210 ch023 sp022 ft021 ho041 hh014 co009 as057, result(r1proxy)
replace r1proxy = 0 if dn038 ==1 ///
										 & ph054 ==1 ///
										 & br017 ==1 ///
										 & hc063 ==1 ///
										 & ep210 ==1 ///
										 & ch023 ==1 ///
										 & sp022 ==1 ///
										 & ft021 ==1 ///
										 & ho041 ==1 ///
										 & hh014 ==1 ///
										 & co009 ==1 ///
										 & as057 ==1
replace r1proxy = 1 if ( inlist(dn038,1,2,3) ///
										 | inlist(ph054,1,2,3) ///
										 | inlist(br017,1,2,3) ///
										 | inlist(hc063,1,2,3) ///
										 | inlist(ep210,1,2,3) ///
										 | inlist(ch023,1,2,3) ///
										 | inlist(sp022,1,2,3) ///
										 | inlist(ft021,1,2,3) ///
										 | inlist(ho041,1,2,3) ///
										 | inlist(hh014,1,2,3) ///
										 | inlist(co009,1,2,3) ///
										 | inlist(as057,1,2,3) ) & missing(r1proxy)
replace r1proxy = 2 if dn038 ==3 ///
										 & ph054 ==3 ///
										 & br017 ==3 ///
										 & hc063 ==3 ///
										 & ep210 ==3 ///
										 & ch023 ==3 ///
										 & sp022 ==3 ///
										 & ft021 ==3 ///
										 & ho041 ==3 ///
										 & hh014 ==3 ///
										 & co009 ==3 ///
										 & as057 ==3
label variable r1proxy "r1proxy:w1 whether r proxy interview"
label values r1proxy proxy

*wave 1 spouse proxy indicator
gen s1proxy =.
spouse r1proxy, result(s1proxy) wave(1)
label variable s1proxy "s1proxy:w1 whether s proxy interview"
label values s1proxy proxy

***interview date***
*wave 1 respondent household interview month
gen r1iwm =.
missing_common int_month, result(r1iwm)
replace r1iwm = int_month if inrange(int_month,1,12)
label variable r1iwm "r1iwm:w1 household interview month"

*wave 1 respondent household interview year
gen r1iwy =.
missing_common int_year, result(r1iwy)
replace r1iwy = int_year if inrange(int_year,2000,2010)
label variable r1iwy "r1iwy:w1 household interview year"

*wave 1 spouse household interview month
gen s1iwm =.
spouse r1iwm, result(s1iwm) wave(1)
label variable s1iwm "s1iwm:w1 household interview month"

*wave 1 spouse household interview year
gen s1iwy =.
spouse r1iwy, result(s1iwy) wave(1)
label variable s1iwy "s1iwy:w1 household interview year"

***birth date***
*respondent birth year
gen rabyear =.
missing_common yrbirth, result(rabyear)
replace rabyear = yrbirth if inrange(yrbirth,1890,2010)
label variable rabyear "rabyear: r birth year"

*wave 1 spouse birth year
gen s1byear =.
spouse rabyear, result(s1byear) wave(1)
label variable s1byear "s1byear: s birth year"

*wave 1 respondent birth month
gen r1bmonth =.
missing_common dn002_, result(r1bmonth)
replace r1bmonth = dn002_ if inrange(dn002_,1,12)

***SHARE sampling eligibility***
*respondent sampling eligibility
gen rasampelig =.
missing_h rabyear, result(rasampelig)
replace rasampelig = 0 if (rabyear >1954  & !missing(rabyear)) | inw1==0
replace rasampelig = 1 if rabyear <=1954 & !missing(rabyear) & inw1==1
label variable rasampelig "rasampelig: r eligibility at w1 sampling"
label values rasampelig share_status

*wave 1 spouse sampling eligibility
gen s1sampelig=.
spouse rasampelig, result(s1sampelig) wave(1)
label variable s1sampelig "s1sampelig:w1 s eligibility at w1 sampling"
label values s1sampelig share_status

****gender***
**respondent gender
*gen ragender =.
*missing_common gender, result(ragender)
*replace ragender = 1 if gender == 1
*replace ragender = 2 if gender == 2
*label variable ragender "ragender: r gender"
*label values ragender gender
*
**wave 1 spouse gender
*gen s1gender =.
*spouse ragender, result(s1gender) wave(1)
*label variable s1gender "s1gender:w`wv' s gender"
*label values s1gender gender

***gender***
*respondent gender
gen ragender =.
missing_common dn042_, result(ragender)
replace ragender = 1 if dn042_ == 1
replace ragender = 2 if dn042_ == 2
label variable ragender "ragender: r gender"
label values ragender gender

*wave 1 spouse gender
gen s1gender =.
spouse ragender, result(s1gender) wave(1)
label variable s1gender "s1gender:w`wv' s gender"
label values s1gender gender

***education: years of education***
*respondent education in years
gen raedyrs =.
replace raedyrs = round(yedu) if inrange(yedu,0,21)
label variable raedyrs "raedyrs: r years of education"

*wave 1 spouse education in years
gen s1edyrs =.
spouse raedyrs, result(s1edyrs) wave(1)
label variable s1edyrs "s1edyrs:w`wv' s years of education"

*respondent years of education flag
gen rafedyrs = .
replace rafedyrs = 0 if yedu_f == 3
replace rafedyrs = 1 if yedu_f == 14
label variable rafedyrs "rafedyrs: r impflag: years of education"
label values rafedyrs i_flag

*wave 1 spouse education in years flag
gen s1fedyrs =.
spouse rafedyrs, result(s1fedyrs) wave(1)
label variable s1fedyrs "s1fedyrs:impflag w`wv' s years of education"
label values s1fedyrs i_flag

***Education: ISCED code***
*respondent education by ISCED code
gen raedisced = .
replace raedisced = isced if inrange(isced,0,6)
label variable raedisced "raedisced: r education by isced code"
label values raedisced edisced

*wave 1 spouse education by ISCED code
gen s1edisced = .
spouse raedisced, result(s1edisced) wave(1)
label variable s1edisced "s1edisced:w1 s education by isced code"
label values s1edisced edisced

*respondent education by ISCED code flag
gen rafedisced = .
replace rafedisced = 0 if isced_f == 3
replace rafedisced = 1 if isced_f == 14
label variable rafedisced "rafedisced:impflag r education by isced code"
label values rafedisced i_flag

*wave 1 spouse education by ISCED code
gen s1fedisced = .
spouse rafedisced, result(s1fedisced) wave(1)
label variable s1fedisced "s1fedisced:impflag w1 s education by isced code"
label values s1fedisced i_flag

****Education: ISCED code***ISCED file
**respondent education by ISCED code
*gen edisced`wv' = .
*replace edisced`wv'=.m if isced1997_r==. & inw`wv'==1
*replace edisced`wv'=.d if isced1997_r==-1
*replace edisced`wv'=.r if isced1997_r==-2
*replace edisced`wv'=.s if isced1997_r==95
*replace edisced`wv'=.o if isced1997_r==97
*replace edisced`wv' = isced1997_r if inrange(isced1997_r,0,6)

***Education, harmonized***
*wave 1 respondent harmonized education
gen raeducl = .
replace raeducl = 1 if inrange(isced,0,2)
replace raeducl = 2 if inrange(isced,3,4)
replace raeducl = 3 if inrange(isced,5,6)
label variable raeducl "raeducl: r harmonized education level"
label values raeducl edharm

*wave 1 spouse harmonized education
gen s`wv'educl = .
spouse raeducl, result(s`wv'educl) wave(`wv')
label variable s`wv'educl "s`wv'educl:w`wv' s harmonized education level"
label values s`wv'educl edharm

***Education, harmonized flag***
*wave 1 respondent flag harmonized education
gen rafeducl = .
replace rafeducl = 0 if isced_f == 3
replace rafeducl = 1 if isced_f == 14
label variable rafeducl "rafeducl:impflag r harmonized education level"
label values rafeducl i_flag

*wave 1 spouse flag harmonized education
gen s`wv'feducl = .
spouse rafeducl, result(s`wv'feducl) wave(`wv')
label variable s`wv'feducl "s`wv'feducl:impflag w`wv' s harmonized education level"
label values s`wv'feducl i_flag

***current marital status: with partnerships***
*wave 1 respondent marital status with partnership
gen r1mstat=.
missing_common dn014_, result(r1mstat)
replace r1mstat = 1 if dn014_ == 1
replace r1mstat = 4 if dn014_ == 3
replace r1mstat = 5 if dn014_ == 5
replace r1mstat = 7 if dn014_ == 6
replace r1mstat = 8 if dn014_ == 4
replace r1mstat = 3 if (dn014_ == 2) | (inlist(r1mstat,4,5,7,8) & inlist(w1spouse,1,2))
label variable r1mstat "r1mstat:w1 r marital status w/partners, filled"
label values r1mstat marwpart

*wave 1 spouse marital status with partnership
gen s1mstat=.
spouse r1mstat, result(s1mstat) wave(1)
label variable s1mstat "s1mstat:w1 s marital status w/partners, filled"
label values  s1mstat marwpart

***current marital status: current implied partnership status***
*wave 1 respondent implied partnership status
gen r1mpart = 0 if inw1 == 1
replace r1mpart = 1 if inlist(dn014_,3,4,5,6) & inlist(w1spouse,1,2)
label variable r1mpart "r1mpart:w1 r implied partnership status"
label values r1mpart part

*wave 1 spouse implied partnership status
gen s1mpart =.
spouse r1mpart, result(s1mpart) wave(1)
label variable s1mpart "s1mpart:w1 s implied partnership status"
label values s1mpart part

***current marital status: without partnerships***
*wave 1 respondent marital status without implied partnership
gen r1mstath =.
missing_common dn014_, result(r1mstath)
replace r1mstath = 1 if dn014_ == 1
replace r1mstath = 3 if dn014_ == 2
replace r1mstath = 4 if dn014_ == 3
replace r1mstath = 5 if dn014_ == 5
replace r1mstath = 7 if dn014_ == 6
replace r1mstath = 8 if dn014_ == 4
label variable r1mstath "r1mstath:w1 r marital status"
label values r1mstath  marwopart

*wave 1 spouse marital status without implied partnership
gen s1mstath =.
spouse r1mstath, result(s1mstath) wave(1)
label variable s1mstath "s1mstath:w1 s marital status"
label values  s1mstath marwopart

***length of current marriage***
*wave 1 respondent length of current marriage
gen r1mcurln =.
missing_common dn015_ dn017_ int_year, result(r1mcurln)
replace r1mcurln = int_year - dn015_ if inrange(dn015_,1900,2020) & inrange(int_year,2000,2020)
replace r1mcurln = int_year - dn017_ if inrange(dn017_,1900,2020) & inrange(int_year,2000,2020)
replace r1mcurln = .i if int_year < dn015_ & inrange(dn015_,1900,2020) & inrange(int_year,2000,2020)
replace r1mcurln = .i if int_year < dn017_ & inrange(dn017_,1900,2020) & inrange(int_year,2000,2020)
label variable r1mcurln "r1mcurln:w1 r length of current marriage"

*wave 1 spouse length of current marriage
gen s1mcurln =.
spouse r1mcurln, result(s1mcurln) wave(1)
label variable s1mcurln "s1mcurln:w1 s length of current marriage"

***Religion***
*Respondent religion
gen rarelig=.
replace rarelig= 1 if q34_re==1 // protestant
replace rarelig= 2 if q34_re==2 // catholic
replace rarelig= 3 if q34_re==4 // jewish
replace rarelig= 4 if q34_re==8 // none/no pref
replace rarelig= 5 if q34_re==7 // other
replace rarelig= 7 if q34_re==5 // muslim
replace rarelig= 8 if q34_re==3 // orthodox
label variable rarelig "rarelig: r religion"
label values rarelig relig

*wave 1 spouse religion
gen s1relig =.
spouse rarelig, result(s1relig) wave(1)
label variable s1relig "s1relig:w1 s religion"
label values s1relig relig

***place of birth***
*respondent place of birth
gen rabplace =.
missing_common dn004_ dn005c, result(rabplace)
replace rabplace = .n if dn005c==-7
replace rabplace = 40 if country==11 & dn004_==1
replace rabplace = 276 if country==12 & dn004_==1
replace rabplace = 752 if country==13 & dn004_==1
replace rabplace = 528 if country==14 & dn004_==1
replace rabplace = 724 if country==15 & dn004_==1
replace rabplace = 380 if country==16 & dn004_==1
replace rabplace = 250 if country==17 & dn004_==1
replace rabplace = 208 if country==18 & dn004_==1
replace rabplace = 300 if country==19 & dn004_==1
replace rabplace = 756 if country==20 & dn004_==1
replace rabplace = 56 if country==23 & dn004_==1
replace rabplace = 376 if country==25 & dn004_==1
replace rabplace = 203 if country==28 & dn004_==1
replace rabplace = 616 if country==29 & dn004_==1
replace rabplace = 372 if country==30 & dn004_==1
replace rabplace = dn005c if inrange(dn005c,1,1500)
label variable rabplace "rabplace: r place of birth"
label values rabplace countries

*wave 1 spouse place of birth
gen s1bplace =.
spouse rabplace, result(s1bplace) wave(1)
label variable s1bplace "s1bplace:w`wv' s place of birth"
label values s1bplace countries

***Whether Born in Country***
*respondent whether born in country
gen rabcountry = .
missing_common dn004_, result(rabcountry)
replace rabcountry = .m if dn004_ == . & inw1==1
replace rabcountry = 0 if dn004_ == 5
replace rabcountry = 1 if dn004_ == 1
replace rabcountry = 0 if !inlist(rabcountry,0,1) & isocountry!=rabplace & !mi(isocountry) & !mi(rabplace)
replace rabcountry = 1 if !inlist(rabcountry,0,1) & isocountry==rabplace & !mi(isocountry) & !mi(rabplace)
label variable rabcountry "rabcountry: r born in country of interview"
label values rabcountry bornin

*wave 1 spouse whether born in country
gen s1bcountry = .
spouse rabcountry, result(s1bcountry) wave(1)
label variable s1bcountry "s1bcountry:w`wv' s born in country of interview"
label values s1bcountry bornin

***rural or urban***
*household wave 1 rural or urban
gen h`wv'rural = .
missing_common iv009_, result(h`wv'rural)
replace h`wv'rural = 0 if inrange(iv009_,1,4)
replace h`wv'rural = 1 if iv009_ == 5
label variable h`wv'rural "h`wv'rural:w`wv' lives in rural or urban"
label values h`wv'rural rural

forvalues p = 1 / 9 {
    bysort hhid1: replace h`wv'rural = h`wv'rural[_n-`p'] if hhid1 == hhid1[_n-`p'] & mi(h`wv'rural) & h`wv'rural[_n-`p'] !=. & inw`wv' == 1 // copy values to other household memembers
    bysort hhid1: replace h`wv'rural = h`wv'rural[_n+`p'] if hhid1 == hhid1[_n+`p'] & mi(h`wv'rural) & h`wv'rural[_n+`p'] !=. & inw`wv' == 1 // copy values to other household memembers
}

***Language of Interview***
*wave 1 respondent language of interview
gen r1lang_s = .
replace r1lang_s = 1 if inlist(language,11,12,20,42)
replace r1lang_s = 2 if language == 13
replace r1lang_s = 3 if language == 14
replace r1lang_s = 4 if inlist(language,15,40)
replace r1lang_s = 5 if inlist(language,16,22)
replace r1lang_s = 6 if inlist(language,17,21,23,41)
replace r1lang_s = 7 if language == 18
replace r1lang_s = 8 if language == 19
replace r1lang_s = 9 if language == 24
replace r1lang_s = 10 if language == 25
replace r1lang_s = 11 if language == 26
replace r1lang_s = 12 if inlist(language,27,37)
replace r1lang_s = 13 if language == 28
replace r1lang_s = 14 if language == 29
replace r1lang_s = 15 if language == 30
replace r1lang_s = 16 if language == 32
replace r1lang_s = 17 if inlist(language,33,43)
replace r1lang_s = 18 if language == 34
replace r1lang_s = 19 if language == 36
replace r1lang_s = 20 if language == 39
replace r1lang_s = 21 if language == 47
replace r1lang_s = 22 if language == 31
replace r1lang_s = 23 if language == 35
replace r1lang_s = 24 if language == 38
replace r1lang_s = 25 if language == 44
replace r1lang_s = 26 if language == 45
label variable r1lang_s "r1lang_s:w1 r language of interview"
label values r1lang_s languages

*wave 1 spouse language of interview
gen s1lang_s = .
spouse r1lang_s, result(s1lang_s) wave(1)
label variable s1lang_s "s1lang_s:w1 s language of interview"
label values s1lang_s languages


***drop cover screen tracker (respondent) file raw variables
drop `demo_all_cv_r'

***drop wave 1 Asset file raw variables
drop `demo_w1_as'

***drop wave 1 Behavioural Risk file raw variables
drop `demo_w1_br'

***drop wave 1 Children file raw variables
drop `demo_w1_ch'

***drop wave 1 Consumption file raw variables
drop `demo_w1_co'

***drop wave 1 Demographic file raw variables
drop `demo_w1_dn'

***drop wave 1 Employment and Pension file raw variables
drop `demo_w1_ep'

***drop wave 1 Financial Transfer file raw variables
drop `demo_w1_ft'

***drop wave 1 Health Care file raw variables
drop `demo_w1_hc'

***drop wave 1 Household Income file raw variables
drop `demo_w1_hh'

***drop wave 1 Housing file raw variables
drop `demo_w1_ho'

***drop wave 1 interviewer file raw variables
drop `demo_w1_iv'

***drop wave 1 Physical Health file raw variables
drop `demo_w1_ph'

***drop wave 1 Social Support file raw variables
drop `demo_w1_sp'

***drop wave 1 Dropoff file raw variables
drop `demo_w1_dropoff'

***drop wave 1 Generated Variables (Weights) file raw variables
drop `demo_w1_gv_weights'

***drop wave 1 Generated Variables (Imputations) file raw variables
drop `demo_w1_imputations'

***drop wave 1 Generated Variables (ISCED) file raw variables
drop `demo_w1_isced'

******************************************************************************************

*set wave number
local wv = 2

***merge with cover screen tracker (respondent) data***
local demo_all_cv_r mergeidp2  int_year_w2 int_month_w2 interview_hh_w2 ///
                fam_resp_w2  fin_resp_w2 hou_resp_w2 partnerinhh_w2 coupleid2 ///
                 gender yrbirth mobirth country waveid_hh firstwave_hh ///
                deadoralive_w1 deadoralive_w2 deceased_year deceased_month deceased_age agep2007
merge 1:1 mergeid using "$tracker", keepusing(`demo_all_cv_r' hhid2 age_int_w2 interview_w2) nogen

***merge with wave 2 Asset data***
local demo_w2_as as057_
merge 1:1 mergeid using "$wave_2_as", keepusing(`demo_w2_as') nogen

***merge with wave 2 Behavioural Risk data***
local demo_w2_br br017_
merge 1:1 mergeid using "$wave_2_br", keepusing(`demo_w2_br') nogen

***merge with wave 2 Children data***
local demo_w2_ch ch023_
merge 1:1 mergeid using "$wave_2_ch", keepusing(`demo_w2_ch') nogen

***merge with wave 2 Consumption data***
local demo_w2_co co009_
merge 1:1 mergeid using "$wave_2_co", keepusing(`demo_w2_co') nogen

***merge with wave 2 Demographic data***
local demo_w2_dn dn041_ dn038_ dn014_ dn044_ dn015_ dn017_ dn004_ dn005c dn002_ language dn042_
merge 1:1 mergeid using "$wave_2_dn", keepusing(`demo_w2_dn') nogen

***merge with wave 2 Employment and Pension data***
local demo_w2_ep ep210_
merge 1:1 mergeid using "$wave_2_ep", keepusing(`demo_w2_ep') nogen

***merge with wave 2 Financial Transfer data***
local demo_w2_ft ft021_
merge 1:1 mergeid using "$wave_2_ft", keepusing(`demo_w2_ft') nogen

***merge with wave 2 Health Care data***
local demo_w2_hc hc063_
merge 1:1 mergeid using "$wave_2_hc", keepusing(`demo_w2_hc') nogen

***merge with wave 2 Household Income data***
local demo_w2_hh hh014_
merge 1:1 mergeid using "$wave_2_hh", keepusing(`demo_w2_hh') nogen

***merge with wave 2 Housing data***
local demo_w2_ho ho041_ 
merge 1:1 mergeid using "$wave_2_ho", keepusing(`demo_w2_ho') nogen

***merge with wave 2 interviewer data***
local demo_w2_iv iv009_ 
merge 1:1 mergeid using "$wave_2_iv", keepusing(`demo_w2_iv') nogen

***merge with wave 2 Physical Health data***
local demo_w2_ph ph054_
merge 1:1 mergeid using "$wave_2_ph", keepusing(`demo_w2_ph') nogen

***merge with wave 2 Social Support data***
local demo_w2_sp sp022_
merge 1:1 mergeid using "$wave_2_sp", keepusing(`demo_w2_sp') nogen

***merge with wave 2 Dropoff data***
local demo_w2_dropoff q3_a
merge 1:1 mergeid using "$wave_2_dropoff", keepusing(`demo_w2_dropoff') nogen

***merge with wave 2 Generated Variables (Weights) data***
local demo_w2_gv_weights cchw_w2 cciw_w2 dw_w2 ///
                         psu ssu stratum1 stratum2
merge 1:1 mergeid using "$wave_2_gv_weights", keepusing(`demo_w2_gv_weights') nogen

***merge with wave 2 Generated Variables (Imputations) data***
local demo_w2_imputations isced isced_f yedu yedu_f implicat p_nrp
merge 1:1 mergeid using "$wave_2_imputations_", keepusing(`demo_w2_imputations') nogen

***merge with Longititudinal Weights wave 1 wave 2 data***
local demo_long_weight_w1w2 clhw_b cliw_b
merge 1:1 mergeid using "$long_weight_w1w2", keepusing(`demo_long_weight_w1w2') nogen

***merge with wave 2 Generated Variables ISCED data***
local demo_w2_isced isced1997_r 
merge 1:1 mergeid using "$wave_2_gv_isced", keepusing(`demo_w2_isced') nogen


***wave status: response indicator***
*wave 1 response indicator (update)
replace inw1 = 0 if missing(inw1)

*wave 2 response indicator
gen inw2 = 0
replace inw2 = 1 if interview_w2 == 1
label variable inw2 "inw2: =1 if respondent w2"
label values inw2 inw

***wave status: drop-off response indicator***
*wave 2 response indicator
gen inw2sc = 0
replace inw2sc = 1 if !mi(q3_a)
label variable inw2sc "inw2sc: =1 if respondent w2 drop-off"
label values inw2sc inw

***person specific identifier***
*country id (char)
replace cid = substr(mergeid,1,2) if missing(cid)
replace cid = "11" if cid == "AT"
replace cid = "12" if cid == "DE"
replace cid = "13" if cid == "SE"
replace cid = "14" if cid == "NL"
replace cid = "15" if cid == "ES" | cid == "Eg"
replace cid = "16" if cid == "IT"
replace cid = "17" if cid == "FR" | cid == "F1"
replace cid = "18" if cid == "DK"
replace cid = "19" if cid == "GR"
replace cid = "20" if cid == "Cg"
replace cid = "21" if cid == "Cf"
replace cid = "22" if cid == "Ci"
replace cid = "23" if cid == "Bf"
replace cid = "24" if cid == "Bn"
replace cid = "25" if cid == "Ih"
replace cid = "26" if cid == "Ia"
replace cid = "27" if cid == "Ir"
replace cid = "28" if cid == "CZ"
replace cid = "29" if cid == "PL"
replace cid = "30" if cid == "IE"
replace cid = "31" if cid == "LU"
replace cid = "32" if cid == "HU"
replace cid = "33" if cid == "PT"
replace cid = "34" if cid == "SI"
replace cid = "35" if cid == "EE"
replace cid = "47" if cid == "HR"

*household id (char) (update)
replace hhidc = substr(mergeid,4,6) if missing(hhidc)

*person number (char) (update)
replace pn = substr(mergeid,11,2) if missing(pn)
*note: pn = a5, a6, b5, b6 are coverscreen respondents outside of the household
*we convert them to numbers to create a numerical version of nmergeid
replace pn = "90" if pn == "a5"
replace pn = "91" if pn == "a6"
replace pn = "92" if pn == "b5"
replace pn = "93" if pn == "b6"

*merge identifier (num) (update)
tempvar w2mergeid
gen `w2mergeid' =cid+hhidc+pn
destring `w2mergeid', replace
replace nmergeid = `w2mergeid' if missing(nmergeid)

***household identifier***
*wave 2 household id (char)
tempvar subhh hhidc
gen `subhh' = substr(hhid2,11,1)
replace `subhh' = "1" if `subhh' == "A"
replace `subhh' = "2" if `subhh' == "B"
gen `hhidc' = substr(hhid2,4,6)
gen hh2hhidc =""
replace hh2hhidc = cid + `hhidc' + `subhh' if inw2 == 1
label variable hh2hhidc "hh2hhidc:w2 cid + hhold id + subhousehold /9-char"

*wave 2 household id (num)
destring hh2hhidc, generate(hh2hhid)
label variable hh2hhid "hh2hhid:w2 cid + hhold id + subhousehold /Num"
format hh2hhid %9.0f
label variable hhid2 "hhid2: SHARE household identifier wave 2"

***Country***
replace racountry = country if mi(racountry)

replace isocountry = 040 if country == 11
replace isocountry = 276 if country == 12
replace isocountry = 752 if country == 13
replace isocountry = 528 if country == 14
replace isocountry = 724 if country == 15
replace isocountry = 380 if country == 16
replace isocountry = 250 if country == 17
replace isocountry = 208 if country == 18
replace isocountry = 300 if country == 19
replace isocountry = 756 if country == 20
replace isocountry = 056 if country == 23
replace isocountry = 376 if country == 25
replace isocountry = 203 if country == 28
replace isocountry = 616 if country == 29
replace isocountry = 372 if country == 30
replace isocountry = 442 if country == 31
replace isocountry = 348 if country == 32
replace isocountry = 620 if country == 33
replace isocountry = 705 if inlist(country, 34, 43)
replace isocountry = 233 if country == 35
replace isocountry = 191 if country == 47



***Implicate***
replace implicate = implicat if mi(implicate)

***couple identifier***
*wave 2 couple id (number)
gen pnp = substr(mergeidp2,11,2)

gen hh_coup=""
levelsof pn if !mi(hhid2), local(pns)
foreach r of local pns {
	forvalues i = 1/9 {
		bysort hhid2: replace hh_coup = "`r'"+"`i'" ///
			if pn == pnp[_n-`i'] & pn == "`r'"
		bysort hhid2: replace hh_coup ="`r'"+"`i'" ///
			if hh_coup[_n+`i'] == "`r'"+"`i'"
	}
}
replace hh_coup=string(_n) if missing(hh_coup)
egen h`wv'coupid = group(hhid2 hh_coup) if !mi(hhid2)
label variable h`wv'coupid "wave `wv' specific couple id number /num"
drop hh_coup pnp

***Spouse Identifier***
*wave 2 spouse id (char)
gen s2mergeid = "0" if inw2 == 1
bysort h2coupid: replace s2mergeid = mergeid[_n+1] if !missing(mergeid[_n+1]) & !missing(h2coupid)
bysort h2coupid: replace s2mergeid = mergeid[_n-1] if !missing(mergeid[_n-1]) & !missing(h2coupid)
label variable s2mergeid "s2mergeid:w2 spouse mergeid /12-char"

*wave 2 spouse id (num)
gen s2nmergeid =""
replace s2nmergeid = "0"  if s2mergeid=="0"

tempvar s2_cid s2_hhidc s2_pn

gen `s2_cid' = substr(s2mergeid,1,2)
replace `s2_cid' = "11" if `s2_cid' == "AT"
replace `s2_cid' = "12" if `s2_cid' == "DE"
replace `s2_cid' = "13" if `s2_cid' == "SE"
replace `s2_cid' = "14" if `s2_cid' == "NL"
replace `s2_cid' = "15" if `s2_cid' == "ES" | `s2_cid' == "Eg"
replace `s2_cid' = "16" if `s2_cid' == "IT"
replace `s2_cid' = "17" if `s2_cid' == "FR" | `s2_cid' == "F1"
replace `s2_cid' = "18" if `s2_cid' == "DK"
replace `s2_cid' = "19" if `s2_cid' == "GR"
replace `s2_cid' = "20" if `s2_cid' == "Cg"
replace `s2_cid' = "21" if `s2_cid' == "Cf"
replace `s2_cid' = "22" if `s2_cid' == "Ci"
replace `s2_cid' = "23" if `s2_cid' == "Bf"
replace `s2_cid' = "24" if `s2_cid' == "Bn"
replace `s2_cid' = "25" if `s2_cid' == "Ih"
replace `s2_cid' = "26" if `s2_cid' == "Ia"
replace `s2_cid' = "27" if `s2_cid' == "Ir"
replace `s2_cid' = "28" if `s2_cid' == "CZ"
replace `s2_cid' = "29" if `s2_cid' == "PL"
replace `s2_cid' = "30" if `s2_cid' == "IE"
replace `s2_cid' = "31" if `s2_cid' == "LU"

gen `s2_hhidc' = substr(s2mergeid,4,6)

gen `s2_pn' = substr(s2mergeid,11,2)

replace s2nmergeid =`s2_cid'+`s2_hhidc'+`s2_pn' if missing(s2nmergeid)
label variable s2nmergeid "s2nmergeid:w2 spouse mergeid /num"
codebook s2nmergeid

*respondent number of spouse id's (update)
replace raspct = raspct + 1 if s2nmergeid != s1nmergeid & !inlist(s2nmergeid,"0","")
replace raspct = 0 if s2nmergeid == "0" & missing(raspct)
replace raspct = 1 if !inlist(s2nmergeid,"0","") & missing(raspct)

*respondent first spouse id (update)
replace raspid1 = s2nmergeid if !inlist(s2nmergeid,"0","") & missing(raspid1)
codebook raspid1

*respondent second spouse id
gen raspid2 =""
replace raspid2 = s2nmergeid if s2nmergeid != raspid1 & !inlist(s2nmergeid,"0","") & !inlist(raspid1,"0","") & mi(raspid2)
label variable raspid2 "raspid2: mergeid of 2nd spouse"
codebook raspid2

*create temporary indicator of wave 2 spouse
gen w`wv'spouse=0 if missing(mergeidp2) & inw`wv'==1
bysort h`wv'coupid: replace w`wv'spouse = 1 if !mi(mergeidp2) & inw`wv'[_n-1] == 1 & inw`wv' == 1 & !mi(h`wv'coupid)
bysort h`wv'coupid: replace w`wv'spouse = 1 if !mi(mergeidp2) & inw`wv'[_n+1] == 1 & inw`wv' == 1 & !mi(h`wv'coupid)
bysort h`wv'coupid: replace w`wv'spouse = 2 if !mi(mergeidp2) & inw`wv'[_n-1] == 0 & inw`wv' == 1 & !mi(h`wv'coupid)
bysort h`wv'coupid: replace w`wv'spouse = 2 if !mi(mergeidp2) & inw`wv'[_n+1] == 0 & inw`wv' == 1 & !mi(h`wv'coupid)

***wave status: interview status***
*wave 1 interview status (update)
replace r1iwstat = 0 if missing(r1iwstat)

*wave 2 interview status
gen r2iwstat =.
replace r2iwstat = 0
replace r2iwstat = 1 if inw2 == 1
replace r2iwstat = 4 if inw2 == 0 & deadoralive_w2 == 1
replace r2iwstat = 5 if inw2 == 0 & deadoralive_w1 == 1 & deadoralive_w2 == 2
replace r2iwstat = 9 if inw2 == 0 & deadoralive_w2 == 0
label variable r2iwstat "r2iwstat:w2 r interview status"
label values r2iwstat wstat

*wave 2 spouse interview status
gen s2iwstat =.
spouse r2iwstat, result(s2iwstat) wave(2)
label variable s2iwstat "s2iwstat:w2 s interview status"
label values s2iwstat wstat

***cohort***
*sample cohort (update)
replace hacohort = 1 if inlist(waveid_hh,62,92) & inlist(country,28,29,30) & mi(hacohort)
replace hacohort = 2 if inlist(waveid_hh,62,92) & inlist(country,12,13,14,15,16,17,18,19,20,23,25) & mi(hacohort)

****Wave in which the household was sampled***
*household wave in which sampled (update)
replace hawvsamp = firstwave_hh if missing(hawvsamp)

***sampling weight***
*sampling weight, combined sample
gen r`wv'wtsamp =.
replace r`wv'wtsamp = dw_w2
label variable r`wv'wtsamp "r`wv'wtsamp:w`wv' design weight"

*respondent psu (update)
replace rapsu1 = real(psu) if mi(rapsu1)

*respondent psu 2 (update)
replace rapsu2 = real(ssu) if mi(rapsu2)

*respondent stratification variable (update)
replace raestrat1 = stratum1 if mi(stratum1)

*respondent stratification variable 2 (update)
replace raestrat2 = stratum2 if mi(raestrat2)

***Household analysis weight***
*wave 2 household analysis weight, combined sample
gen r`wv'wthh =.
replace r`wv'wthh = cchw_w2
label variable r`wv'wthh "r`wv'wthh:w`wv' household analysis weight"

***person-level analysis weight***
*wave 2 respondent analysis weight, combined sample
gen r`wv'wtresp =.
replace r`wv'wtresp = cciw_w2
label variable r`wv'wtresp "r`wv'wtresp:w`wv' person-level analysis weight"

***household longitudinal weight***
*household longitudinal weight, combined sample
gen ral12wthh =.
replace ral12wthh = clhw_b
label variable ral12wthh "ral12wthh:w`wv' household longitudinal w1-w2 weight"

***person-level longitudinal weight***
*respondent longitudinal weight, combined sample
gen ral12wtrsp =.
replace ral12wtrsp = cliw_b
label variable ral12wtrsp "ral12wtrsp:w`wv' person-level longitudinal w1-w2 weight"

***number of household respondents***     
*wave 2 household number of respondents 
egen hh2hhresp = sum(inw2==1) if inw2==1, by(hh2hhid)
label variable hh2hhresp "hh2hhresp:w2 # core respondents in hh"

***whether coupled***
*wave 2 couple indicator
egen h2cpl = sum(inw2==1) if inw2==1, by(h2coupid)
recode h2cpl (0=.) (1=0) (2=1)
label variable h2cpl "h2cpl:w2 whether coupled"
label values h2cpl cpl

***financial respondent***
*wave 1 respondent financial respondent indicator
gen r`wv'finr =.
replace r`wv'finr = 0 if fin_resp_w2 == 0
replace r`wv'finr = 1 if fin_resp_w2 == 1
label variable r`wv'finr "r`wv'finr:w`wv' whether r financial resp"
label values r`wv'finr finr

*wave 1 spouse financial respondent indicator
gen s`wv'finr =.
spouse r`wv'finr, result(s`wv'finr) wave(`wv')
label variable s`wv'finr "s`wv'finr:w`wv' whether s financial resp"
label values s`wv'finr  finr

***hosuehold respondent***
*wave 1 respondent household respondent indicator
gen r`wv'hhr =.
replace r`wv'hhr = 1 if hou_resp_w2 == 1
replace r`wv'hhr = 0 if hou_resp_w2 == 0
label variable r`wv'hhr "r`wv'hhr:w`wv' whether r hh resp"
label values r`wv'hhr hhr

*wave 1 spouse household respondent indicator
gen s`wv'hhr =.
spouse r`wv'hhr, result(s`wv'hhr) wave(`wv')
label variable s`wv'hhr "s`wv'hhr:w`wv' whether s hh resp"
label values s`wv'hhr hhr

***family respondent***
*wave 1 respondent family respondent indicator
gen r`wv'famr =.
replace r`wv'famr = 1 if fam_resp_w2 == 1
replace r`wv'famr = 0 if fam_resp_w2 == 0
label variable r`wv'famr "r`wv'famr:w`wv' whether r family resp"
label values r`wv'famr   famr

*wave 1 spouse family respondent indicator
gen s`wv'famr =.
spouse r`wv'famr, result(s`wv'famr) wave(`wv')
label variable s`wv'famr "s`wv'famr:w`wv' whether s family resp"
label values s`wv'famr  famr

***whether proxy interview***
*wave 2 respondent proxy indicator
gen r2proxy =.
missing_common dn038 ph054 br017 hc063 ep210 ch023 sp022 ft021 ho041 hh014 co009 as057, result(r2proxy)
replace r2proxy = 0 if dn038 ==1 ///
					 & ph054 ==1 ///
					 & br017 ==1 ///
					 & hc063 ==1 ///
					 & ep210 ==1 ///
					 & ch023 ==1 ///
					 & sp022 ==1 ///
					 & ft021 ==1 ///
					 & ho041 ==1 ///
					 & hh014 ==1 ///
					 & co009 ==1 ///
					 & as057 ==1
replace r2proxy = 1 if ( inlist(dn038,1,2,3) ///
						 | inlist(ph054,1,2,3) ///
						 | inlist(br017,1,2,3) ///
						 | inlist(hc063,1,2,3) ///
						 | inlist(ep210,1,2,3) ///
						 | inlist(ch023,1,2,3) ///
						 | inlist(sp022,1,2,3) ///
						 | inlist(ft021,1,2,3) ///
						 | inlist(ho041,1,2,3) ///
						 | inlist(hh014,1,2,3) ///
						 | inlist(co009,1,2,3) ///
						 | inlist(as057,1,2,3) ) & missing(r2proxy)
replace r2proxy = 2 if dn038 ==3 ///
					 & ph054 ==3 ///
					 & br017 ==3 ///
					 & hc063 ==3 ///
					 & ep210 ==3 ///
					 & ch023 ==3 ///
					 & sp022 ==3 ///
					 & ft021 ==3 ///
					 & ho041 ==3 ///
					 & hh014 ==3 ///
					 & co009 ==3 ///
					 & as057 ==3
label variable r2proxy "r2proxy:w2 whether r proxy interview"

*wave 2 spouse proxy indicator
gen s2proxy =.
spouse r2proxy, result(s2proxy) wave(2)
label variable s2proxy "s2proxy:w2 whether s proxy interview"
label values r2proxy proxy
label values s2proxy proxy

***interview date***
*wave 2 respondent household interview month
gen r2iwm =.
missing_common int_month_w2 if interview_w2 == 1, result(r2iwm)
replace r2iwm = int_month_w2 if inrange(int_month_w2,1,12) & interview_w2 == 1
label variable r2iwm "r2iwm:w2 household interview month"

*wave 2 respondent household interview year
gen r2iwy =.
missing_common int_year_w2 if interview_w2 == 1, result(r2iwy)
replace r2iwy = int_year_w2 if inrange(int_year_w2,2000, 2010) & interview_w2 == 1
label variable r2iwy "r2iwy:w2 household interview year"

*wave 2 spouse household interview month
gen s2iwm =.
spouse r2iwm, result(s2iwm) wave(2)
label variable s2iwm "s2iwm:w2 household interview month"

*wave 2 spouse household interview year
gen s2iwy =.
spouse r2iwy, result(s2iwy) wave(2)
label variable s2iwy "s2iwy:w2 household interview year"

***birth date***
*respondent birth year (update)
missing_common yrbirth  if missing(rabyear), result(rabyear)
replace rabyear = yrbirth if inrange(yrbirth,1890,2010) & missing(rabyear)

*wave 2 spouse birth year
gen s2byear =.
spouse rabyear, result(s2byear) wave(2)
label variable s2byear "s2byear: s birth year"

*wave 2 respondent birth month
gen r2bmonth =.
missing_common dn002_, result(r2bmonth)
replace r2bmonth = dn002_ if inrange(dn002_,1,12)

***death date***
*respondent death year
gen radyear =.
missing_common deceased_year, result(radyear)
replace radyear = .i if deceased_year == -3
replace radyear = deceased_year if inrange(deceased_year,2000,2020)
label variable radyear "radyear: r death year"

*respondent death month
gen radmonth =.
missing_common deceased_month, result(radmonth)
replace radmonth = .i if deceased_month == -3
replace radmonth = deceased_month if inrange(deceased_month,1,12)
label variable radmonth "radmonth: r death month"

***SHARE sampling eligibility***
*wave 2 spouse sampling eligibility
gen s2sampelig=.
spouse rasampelig, result(s2sampelig) wave(2)
label variable s2sampelig "s2sampelig:w2 s eligibility at w1 sampling"
label values s2sampelig share_status

****gender***
**respondent gender (update)
*missing_common gender if missing(ragender), result(ragender)
*replace ragender = 1 if gender == 1 & missing(ragender)
*replace ragender = 2 if gender == 2 & missing(ragender)
*
**wave 2 spouse gender
*gen s2gender =.
*spouse ragender, result(s2gender) wave(2)
*label variable s2gender "s2gender:w`wv' s gender"
*label values s2gender gender

***gender***
*respondent gender (update)
missing_common dn042_ if missing(ragender), result(ragender)
replace ragender = 1 if dn042_ == 1 & missing(ragender)
replace ragender = 2 if dn042_ == 2 & missing(ragender)

*wave 2 spouse gender
gen s2gender =.
spouse ragender, result(s2gender) wave(2)
replace s2gender = .v if s`wv'iwstat==4
label variable s2gender "s2gender:w`wv' s gender"
label values s2gender gender

***education: years of education***
*respondent education in years (update)
replace raedyrs = round(yedu) if inrange(yedu,0,21) & mi(raedyrs)

*wave 2 spouse education in years
gen s2edyrs =.
spouse raedyrs, result(s2edyrs) wave(2)
label variable s2edyrs "s2edyrs:w`wv' s years of education"

*respondent years of education flag (update)
replace rafedyrs = 0 if yedu_f == 3 & mi(rafedyrs)
replace rafedyrs = 1 if yedu_f == 14 & mi(rafedyrs)

*wave 2 spouse education in years flag
gen s2fedyrs =.
spouse rafedyrs, result(s2fedyrs) wave(2)
label variable s2fedyrs "s2fedyrs:impflag w`wv' s years of education"
label values s2fedyrs i_flag

***Education: ISCED code***
*respondent education by ISCED code (update)
replace raedisced = isced if inrange(isced,0,6) & mi(raedisced)

*wave 2 spouse education by ISCED code
gen s2edisced = .
spouse raedisced, result(s2edisced) wave(2)
label variable s2edisced "s2edisced:w2 s education by isced code"
label values s2edisced edisced

*respondent education by ISCED code flag (update)
replace rafedisced = 0 if isced_f == 3 & mi(rafedisced)
replace rafedisced = 1 if isced_f == 14 & mi(rafedisced)

*wave 2 spouse education by ISCED code
gen s2fedisced = .
spouse rafedisced, result(s2fedisced) wave(2)
label variable s2fedisced "s2fedisced:impflag w2 s education by isced code"
label values s2fedisced i_flag

****Education: ISCED code***ISCED file
**respondent education by ISCED code
*gen edisced`wv' = .
*replace edisced`wv'=.m if isced1997_r==. & inw`wv'==1
*replace edisced`wv'=.d if isced1997_r==-1
*replace edisced`wv'=.r if isced1997_r==-2
*replace edisced`wv'=.s if isced1997_r==95
*replace edisced`wv'=.o if isced1997_r==97
*replace edisced`wv' = isced1997_r if inrange(isced1997_r,0,6)

***Education, harmonized***
*respondent education harmonized (update)
replace raeducl = 1 if inrange(isced,0,2) & mi(raeducl)
replace raeducl = 2 if inrange(isced,3,4) & mi(raeducl)
replace raeducl = 3 if inrange(isced,5,6) & mi(raeducl)

*wave 2 spouse education harmonized
gen s2educl =.
spouse raeducl, result(s2educl) wave(2)
label variable s2educl "s2educl:w2 s harmonized education level"
label values s2educl edharm

*respondent education harmonized flag (update)
replace rafeducl = 0 if isced_f == 3 & mi(rafeducl)
replace rafeducl = 1 if isced_f == 14 & mi(rafeducl)

*wave 2 spouse education harmonized
gen s2feducl =.
spouse rafeducl, result(s2feducl) wave(2)
label variable s2feducl "s2feducl:impflag w2 s harmonized education level"
label values s2feducl i_flag

***current marital status: with partnerships***
*wave 2 respondent marital status with partnership
gen r2mstat=.
missing_common dn044_ dn014_, result(r2mstat)
replace r2mstat = r1mstat if r1mstat != . & dn044_ == 5
replace r2mstat = 1 if dn014_ == 1
replace r2mstat = 4 if dn014_ == 3
replace r2mstat = 5 if dn014_ == 5
replace r2mstat = 7 if dn014_ == 6
replace r2mstat = 8 if dn014_ == 4
replace r2mstat = 3 if (dn014_ == 2) | (inlist(r2mstat,4,5,7,8) & inlist(w2spouse,1,2))
label variable r2mstat "r2mstat:w2 r marital status w/partners, filled"

*wave 2 spouse marital status with partnership
gen s2mstat=.
spouse r2mstat, result(s2mstat) wave(2)
label variable s2mstat "s2mstat:w2 s marital status w/partners, filled"
label values r2mstat marwpart
label values s2mstat marwpart

***current marital status: current implied partnership status***
*wave 2 respondent implied partnership status
gen r2mpart = 0 if inw2==1
replace r2mpart = 1 if inlist(dn014_, 3, 4, 5, 6) & inlist(w2spouse,1,2)
label variable r2mpart "r2mpart:w2 r implied partnership status"

*wave 2 spouse implied partnership status
gen s2mpart =.
spouse r2mpart, result(s2mpart) wave(2)
label variable s2mpart "s2mpart:w2 s implied partnership status"
label values r2mpart part
label values s2mpart part

***current marital status: without implied partnerships***
*wave 2 respondent marital status without implied partnership
gen r2mstath =.
missing_common dn014_, result(r2mstath)
replace r2mstath = r1mstath if r1mstath != . & dn044_ == 5
replace r2mstath = 1 if dn014_ == 1
replace r2mstath = 3 if dn014_ == 2
replace r2mstath = 4 if dn014_ == 3
replace r2mstath = 5 if dn014_ == 5
replace r2mstath = 7 if dn014_ == 6
replace r2mstath = 8 if dn014_ == 4
label variable r2mstath "r2mstath:w2 r marital status"

*wave 2 spouse marital status without implied partnership
gen s2mstath =.
spouse r2mstath, result(s2mstath) wave(2)
label variable s2mstath "s2mstath:w2 s marital status"
label values r2mstath  marwopart
label values s2mstath marwopart

***length of current marriage***
*wave 2 respondent length of current marriage
gen r2mcurln =.
missing_common dn015_ dn017_ int_year_w2, result(r2mcurln)
replace r2mcurln = int_year_w2 - dn015_ if inrange(dn015_,1900,2020) & inrange(int_year_w2,2000,2020)
replace r2mcurln = int_year_w2 - dn017_ if inrange(dn017_,1900,2020) & inrange(int_year_w2,2000,2020)
replace r2mcurln = .i if int_year_w2 < dn015_ & inrange(dn015_,1900,2020) & inrange(int_year_w2,2000,2020)
replace r2mcurln = .i if int_year_w2 < dn017_ & inrange(dn017_,1900,2020) & inrange(int_year_w2,2000,2020)
label variable r2mcurln "r2mcurln:w2 r length of current marriage"

*wave 2 spouse length of current marriage
gen s2mcurln =.
spouse r2mcurln, result(s2mcurln) wave(2)
label variable s2mcurln "s2mcurln:w2 s length of current marriage"

***place of birth***
*respondent place of birth (update)
missing_common dn004_ dn005c if missing(rabplace), result(rabplace)
replace rabplace = .n if dn005c == -7 & missing(rabplace)
replace rabplace = 40 if country==11 & dn004_==1 & missing(rabplace)
replace rabplace = 276 if country==12 & dn004_==1 & missing(rabplace)
replace rabplace = 752 if country==13 & dn004_==1 & missing(rabplace)
replace rabplace = 528 if country==14 & dn004_==1 & missing(rabplace)
replace rabplace = 724 if country==15 & dn004_==1 & missing(rabplace)
replace rabplace = 380 if country==16 & dn004_==1 & missing(rabplace)
replace rabplace = 250 if country==17 & dn004_==1 & missing(rabplace)
replace rabplace = 208 if country==18 & dn004_==1 & missing(rabplace)
replace rabplace = 300 if country==19 & dn004_==1 & missing(rabplace)
replace rabplace = 756 if country==20 & dn004_==1 & missing(rabplace)
replace rabplace = 56 if country==23 & dn004_==1 & missing(rabplace)
replace rabplace = 376 if country==25 & dn004_==1 & missing(rabplace)
replace rabplace = 203 if country==28 & dn004_==1 & missing(rabplace)
replace rabplace = 616 if country==29 & dn004_==1 & missing(rabplace)
replace rabplace = 372 if country==30 & dn004_==1 & missing(rabplace)
replace rabplace = dn005c if inrange(dn005c,1,1500)  & missing(rabplace)

*wave 2 spouse place of birth
gen s2bplace =.
spouse rabplace, result(s2bplace) wave(2)
label variable s2bplace "s2bplace:w`wv' s place of birth"
label values s2bplace countries

***Whether Born in Country***
*respondent whether born in country
missing_common dn004_ if mi(rabcountry), result(rabcountry)
replace rabcountry = .m if dn004_ == . & inw2==1 & !inlist(rabcountry,0,1)
replace rabcountry = 0 if dn004_ == 5 & !inlist(rabcountry,0,1)
replace rabcountry = 1 if dn004_ == 1 & !inlist(rabcountry,0,1)
replace rabcountry = 0 if !inlist(rabcountry,0,1) & isocountry!=rabplace & !mi(isocountry) & !mi(rabplace)
replace rabcountry = 1 if !inlist(rabcountry,0,1) & isocountry==rabplace & !mi(isocountry) & !mi(rabplace)
label variable rabcountry "rabcountry: r born in country of interview"
label values rabcountry bornin

*wave 2 spouse whether born in country
gen s`wv'bcountry = .
spouse rabcountry, result(s`wv'bcountry) wave(`wv')
label variable s`wv'bcountry "s`wv'bcountry:w`wv' s born in country of interview"
label values s`wv'bcountry bornin

***rural or urban***
*household wave 2 rural or urban
gen h`wv'rural = .
missing_common iv009_, result(h`wv'rural)
replace h`wv'rural = .m if iv009_ == . & inw`wv' == 1
replace h`wv'rural = 0 if inrange(iv009_,1,4)
replace h`wv'rural = 1 if iv009_ == 5
label variable h`wv'rural "h`wv'rural:w`wv' lives in rural or urban"
label values h`wv'rural rural

forvalues p = 1 / 9 {
    bysort hhid2: replace h`wv'rural = h`wv'rural[_n-`p'] if hhid2 == hhid2[_n-`p'] & mi(h`wv'rural) & h`wv'rural[_n-`p'] !=. & inw`wv' == 1 // copy values to other household memembers
    bysort hhid2: replace h`wv'rural = h`wv'rural[_n+`p'] if hhid2 == hhid2[_n+`p'] & mi(h`wv'rural) & h`wv'rural[_n+`p'] !=. & inw`wv' == 1 // copy values to other household memembers
}

***Language of Interview***
*wave 2 respondent language of interview
gen r`wv'lang_s = .
replace r`wv'lang_s = 1 if inlist(language,11,12,20,42)
replace r`wv'lang_s = 2 if language == 13
replace r`wv'lang_s = 3 if language == 14
replace r`wv'lang_s = 4 if inlist(language,15,40)
replace r`wv'lang_s = 5 if inlist(language,16,22)
replace r`wv'lang_s = 6 if inlist(language,17,21,23,41)
replace r`wv'lang_s = 7 if language == 18
replace r`wv'lang_s = 8 if language == 19
replace r`wv'lang_s = 9 if language == 24
replace r`wv'lang_s = 10 if language == 25
replace r`wv'lang_s = 11 if language == 26
replace r`wv'lang_s = 12 if inlist(language,27,37)
replace r`wv'lang_s = 13 if language == 28
replace r`wv'lang_s = 14 if language == 29
replace r`wv'lang_s = 15 if language == 30
replace r`wv'lang_s = 16 if language == 32
replace r`wv'lang_s = 17 if inlist(language,33,43)
replace r`wv'lang_s = 18 if language == 34
replace r`wv'lang_s = 19 if language == 36
replace r`wv'lang_s = 20 if language == 39
replace r`wv'lang_s = 21 if language == 47
replace r`wv'lang_s = 22 if language == 31
replace r`wv'lang_s = 23 if language == 35
replace r`wv'lang_s = 24 if language == 38
replace r`wv'lang_s = 25 if language == 44
replace r`wv'lang_s = 26 if language == 45
label variable r`wv'lang_s "r`wv'lang_s:w`wv' r language of interview"
label values r`wv'lang_s languages

*wave 2 spouse language of interview
gen s`wv'lang_s = .
spouse r`wv'lang_s, result(s`wv'lang_s) wave(`wv')
label variable s`wv'lang_s "s`wv'lang_s:w`wv' s language of interview"
label values s`wv'lang_s languages


***drop cover screen tracker (respondent) file raw variables
drop `demo_all_cv_r'

***drop wave 2 Asset file raw variables
drop `demo_w2_as'

***drop wave 2 Behavioural Risk file raw variables
drop `demo_w2_br'

***drop wave 2 Children file raw variables
drop `demo_w2_ch'

***drop wave 2 Consumption file raw variables
drop `demo_w2_co'

***drop wave 2 Demographic file raw variables
drop `demo_w2_dn'

***drop wave 2 Employment and Pension file raw variables
drop `demo_w2_ep'

***drop wave 2 Financial Transfer file raw variables
drop `demo_w2_ft'

***drop wave 2 Health Care file raw variables
drop `demo_w2_hc'

***drop wave 2 Household Income file raw variables
drop `demo_w2_hh'

***drop wave 2 Housing file raw variables
drop `demo_w2_ho'

***drop wave 2 interviewer file raw variables
drop `demo_w2_iv'

***drop wave 2 Physical Health file raw variables
drop `demo_w2_ph'

***drop wave 2 Social Support file raw variables
drop `demo_w2_sp'

***drop wave 2 Dropoff file raw variables
drop `demo_w2_dropoff'

***drop wave 2 Generated Variables (Weights) file raw variables
drop `demo_w2_gv_weights'

***drop Longititudinal Weights wave 1 wave 2 file raw variables
drop `demo_long_weight_w1w2'

***drop wave 2 Generated Variables (Imputations) file raw variables
drop `demo_w2_imputations'

***drop wave 2 Generated Variables (ISCED) file raw variables
drop `demo_w2_isced'

******************************************************************************************

*set wave number
local wv = 3

***merge with cover screen tracker (respondent) data***
local demo_all_cv_r mergeidp3 interview_w3 int_year_w3 int_month_w3 interview_hh_w3 ///
                coupleid3 gender yrbirth mobirth country waveid_hh firstwave_hh ///
                deadoralive_w2 deadoralive_w3 deceased_year deceased_month deceased_age language
merge 1:1 mergeid using "$tracker", keepusing(`demo_all_cv_r' hhid3) nogen


***merge with wave 3 Generated Variables (Weights) data***
local demo_w3_gv_weights cchw_w3 cciw_w3 dw_w3 ///
                         psu ssu stratum1 stratum2
merge 1:1 mergeid using "$wave_3_gv_weights", keepusing(`demo_w3_gv_weights') nogen

***merge with Longititudinal Weights wave 2 wave 3 data***
local demo_long_weight_w2w3 clhw_c cliw_c
merge 1:1 mergeid using "$long_weight_w2w3", keepusing(`demo_long_weight_w2w3') nogen


***wave status: response indicator***
*wave 1 response indicator (update)
replace inw1 = 0 if missing(inw1)

*wave 2 response indicator (update)
replace inw2 = 0 if missing(inw2)

*wave 3 response indicator
gen inw3 = 0
replace inw3 = 1 if interview_w3 == 1
label variable inw3 "inw3: =1 if respondent w3"
label values inw3 inw

***person specific identifier***
*country id (char)
replace cid = substr(mergeid,1,2) if missing(cid)
replace cid = "11" if cid == "AT"
replace cid = "12" if cid == "DE"
replace cid = "13" if cid == "SE"
replace cid = "14" if cid == "NL"
replace cid = "15" if cid == "ES" | cid == "Eg"
replace cid = "16" if cid == "IT"
replace cid = "17" if cid == "FR" | cid == "F1"
replace cid = "18" if cid == "DK"
replace cid = "19" if cid == "GR"
replace cid = "20" if cid == "Cg"
replace cid = "21" if cid == "Cf"
replace cid = "22" if cid == "Ci"
replace cid = "23" if cid == "Bf"
replace cid = "24" if cid == "Bn"
replace cid = "25" if cid == "Ih"
replace cid = "26" if cid == "Ia"
replace cid = "27" if cid == "Ir"
replace cid = "28" if cid == "CZ"
replace cid = "29" if cid == "PL"
replace cid = "30" if cid == "IE"
replace cid = "31" if cid == "LU"
replace cid = "32" if cid == "HU"
replace cid = "33" if cid == "PT"
replace cid = "34" if cid == "SI"
replace cid = "35" if cid == "EE"
replace cid = "47" if cid == "HR"

*household id (char) (update)
replace hhidc = substr(mergeid,4,6) if missing(hhidc)

*person number (char) (update)
replace pn = substr(mergeid,11,2) if missing(pn)
*note: pn = a5, a6, b5, b6 are coverscreen respondents outside of the household
*we convert them to numbers to create a numerical version of nmergeid
replace pn = "90" if pn == "a5"
replace pn = "91" if pn == "a6"
replace pn = "92" if pn == "b5"
replace pn = "93" if pn == "b6"

*merge identifier (num) (update)
tempvar w3mergeid
gen `w3mergeid' =cid+hhidc+pn
destring `w3mergeid', replace
replace nmergeid = `w3mergeid' if missing(nmergeid)

***household identifier***
*wave 3 household id (char)
tempvar subhh hhidc
gen `subhh' = substr(hhid3,11,1)
replace `subhh' = "1" if `subhh'=="A"
replace `subhh' = "2" if `subhh'=="B"
gen `hhidc' = substr(hhid3,4,6)
gen hh3hhidc =""
replace hh3hhidc = cid + `hhidc' + `subhh' if inw3 == 1
label variable hh3hhidc "hh3hhidc:w3 cid + hhold id + subhousehold /9-Char"

*wave 3 household id (num)
destring hh3hhidc, generate(hh3hhid)
label variable hh3hhid "hh3hhid:w3 cid + hhold id + subhousehold /Num"
format hh3hhid %9.0f
label variable hhid3 "hhid3: SHARE household identifier wave 3"

***Country*** (update)
replace racountry = country if mi(racountry)

replace isocountry = 040 if country == 11
replace isocountry = 276 if country == 12
replace isocountry = 752 if country == 13
replace isocountry = 528 if country == 14
replace isocountry = 724 if country == 15
replace isocountry = 380 if country == 16
replace isocountry = 250 if country == 17
replace isocountry = 208 if country == 18
replace isocountry = 300 if country == 19
replace isocountry = 756 if country == 20
replace isocountry = 056 if country == 23
replace isocountry = 376 if country == 25
replace isocountry = 203 if country == 28
replace isocountry = 616 if country == 29
replace isocountry = 372 if country == 30
replace isocountry = 348 if country == 32
replace isocountry = 620 if country == 33
replace isocountry = 705 if inlist(country, 34, 43)
replace isocountry = 233 if country == 35
replace isocountry = 191 if country == 47



***couple identifier***
*wave 3 couple id (number)
gen pnp = substr(mergeidp3,11,2)

gen hh_coup=""
levelsof pn if !mi(hhid3), local(pns)
foreach r of local pns {
	forvalues i = 1/9 {
		bysort hhid3: replace hh_coup = "`r'"+"`i'" ///
			if pn == pnp[_n-`i'] & pn == "`r'"
		bysort hhid3: replace hh_coup ="`r'"+"`i'" ///
			if hh_coup[_n+`i'] == "`r'"+"`i'"
	}
}
replace hh_coup=string(_n) if missing(hh_coup)
egen h`wv'coupid = group(hhid3 hh_coup) if !mi(hhid3)
label variable h`wv'coupid "wave `wv' specific couple id number /num"
drop hh_coup pnp

***Spouse Identifier***
*wave 3 spouse id (char)
gen s3mergeid = "0" if inw3 == 1
bysort h3coupid: replace s3mergeid = mergeid[_n+1] if !missing(mergeid[_n+1]) & !missing(h3coupid)
bysort h3coupid: replace s3mergeid = mergeid[_n-1] if !missing(mergeid[_n-1]) & !missing(h3coupid)
label variable s3mergeid "s3mergeid:w3 spouse mergeid /12-Char"

*wave 3 spouse id (num)
gen s3nmergeid =""
replace s3nmergeid = "0"  if s3mergeid=="0"

tempvar s3_cid s3_hhidc s3_pn

gen `s3_cid' = substr(s3mergeid,1,2)
replace `s3_cid' = "11" if `s3_cid' == "AT"
replace `s3_cid' = "12" if `s3_cid' == "DE"
replace `s3_cid' = "13" if `s3_cid' == "SE"
replace `s3_cid' = "14" if `s3_cid' == "NL"
replace `s3_cid' = "15" if `s3_cid' == "ES" | `s3_cid' == "Eg"
replace `s3_cid' = "16" if `s3_cid' == "IT"
replace `s3_cid' = "17" if `s3_cid' == "FR" | `s3_cid' == "F1"
replace `s3_cid' = "18" if `s3_cid' == "DK"
replace `s3_cid' = "19" if `s3_cid' == "GR"
replace `s3_cid' = "20" if `s3_cid' == "Cg"
replace `s3_cid' = "21" if `s3_cid' == "Cf"
replace `s3_cid' = "22" if `s3_cid' == "Ci"
replace `s3_cid' = "23" if `s3_cid' == "Bf"
replace `s3_cid' = "24" if `s3_cid' == "Bn"
replace `s3_cid' = "25" if `s3_cid' == "Ih"
replace `s3_cid' = "26" if `s3_cid' == "Ia"
replace `s3_cid' = "27" if `s3_cid' == "Ir"
replace `s3_cid' = "28" if `s3_cid' == "CZ"
replace `s3_cid' = "29" if `s3_cid' == "PL"
replace `s3_cid' = "30" if `s3_cid' == "IE"
replace `s3_cid' = "31" if `s3_cid' == "LU"
replace `s3_cid' = "32" if `s3_cid' == "HU"
replace `s3_cid' = "33" if `s3_cid' == "PT"
replace `s3_cid' = "34" if `s3_cid' == "SI"
replace `s3_cid' = "35" if `s3_cid' == "EE"

gen `s3_hhidc' = substr(s3mergeid,4,6)

gen `s3_pn' = substr(s3mergeid,11,2)

replace s3nmergeid =`s3_cid'+`s3_hhidc'+`s3_pn' if missing(s3nmergeid)
label variable s3nmergeid "s3nmergeid:w3 spouse mergeid /num"

*respondent number of spouse id's (update)
replace raspct = raspct + 1 if s3nmergeid != s1nmergeid & s3nmergeid != s2nmergeid & !inlist(s3nmergeid,"0","")
replace raspct = 0 if s3nmergeid == "0" & missing(raspct)
replace raspct = 1 if !inlist(s3nmergeid,"0","") & missing(raspct)

*respondent first spouse id (update)
replace raspid1 = s3nmergeid if !inlist(s3nmergeid,"0","") & missing(raspid1)
codebook raspid1

*respondent second spouse id (update)
replace raspid2 = s3nmergeid if s3nmergeid != raspid1 & !inlist(raspid1,"0","") & ///
                                !inlist(s3nmergeid,"0","") & mi(raspid2)
codebook raspid2

*respondent third spouse id
gen raspid3 =""
replace raspid3 = s3nmergeid if s3nmergeid != raspid1 & !inlist(raspid1,"0","") & ///
                                s3nmergeid != raspid2 & !inlist(raspid2,"0","") & /// 
                                !inlist(s3nmergeid,"0","") & mi(raspid3)
label variable raspid3 "raspid3: mergeid of 3rd spouse"
codebook raspid3


*create temporary indicator of wave 3 spouse
gen w3spouse=0 if missing(mergeidp3) & inw3==1
bysort h3coupid: replace w3spouse = 1 if !mi(mergeidp3) & inw3[_n-1] == 1 & inw3 == 1 & !mi(h3coupid)
bysort h3coupid: replace w3spouse = 1 if !mi(mergeidp3) & inw3[_n+1] == 1 & inw3 == 1 & !mi(h3coupid)
bysort h3coupid: replace w3spouse = 2 if !mi(mergeidp3) & inw3[_n-1] == 0 & inw3 == 1 & !mi(h3coupid)
bysort h3coupid: replace w3spouse = 2 if !mi(mergeidp3) & inw3[_n+1] == 0 & inw3 == 1 & !mi(h3coupid)

***wave status: interview status***
*wave 1 interview status (update)
replace r1iwstat = 0 if missing(r1iwstat)

*wave 2 interview status (update)
replace r2iwstat = 0 if missing(r2iwstat)

*wave 3 interview status
gen r3iwstat = .
replace r3iwstat = 0
replace r3iwstat = 1 if inw3 == 1
replace r3iwstat = 4 if inw3 == 0 & deadoralive_w3 == 1
replace r3iwstat = 5 if inw3 == 0 & deadoralive_w2 == 1 & deadoralive_w3 == 2
replace r3iwstat = 6 if inw3 == 0 & deadoralive_w2 == 2 & deadoralive_w3 == 2
replace r3iwstat = 9 if inw3 == 0 & deadoralive_w3 == 0
label variable r3iwstat "r3iwstat:w3 r interview status"
label values r3iwstat wstat

*wave 3 spouse interview status
gen s3iwstat = .
spouse r3iwstat, result(s3iwstat) wave(3)
label variable s3iwstat "s3iwstat:w3 s interview status"
label values s3iwstat wstat

***cohort***
*sample cohort (update)
replace hacohort = 1 if inlist(waveid_hh,42,52) & mi(hacohort)
replace hacohort = 2 if inlist(waveid_hh,62,92) & mi(hacohort)

****Wave in which the household was sampled***
*household wave in which sampled (update)
replace hawvsamp = firstwave_hh if missing(hawvsamp)

***sampling weight***
*sampling weight, combined sample
gen r`wv'wtsamp =.
replace r`wv'wtsamp = dw_w3
label variable r`wv'wtsamp "r`wv'wtsamp:w`wv' design weight"

*respondent psu (update)
replace rapsu1 = real(psu) if mi(rapsu1)

*respondent psu 2 (update)
replace rapsu2 = real(ssu) if mi(rapsu2)

*respondent stratification variable (update)
replace raestrat1 = stratum1 if mi(stratum1)

*respondent stratification variable 2 (update)
replace raestrat2 = stratum2 if mi(raestrat2)

***household longitudinal weight***
*household longitudinal weight, combined sample
gen ral23wthh =.
replace ral23wthh = clhw_c
label variable ral23wthh "ral23wthh:w`wv' household longitudinal w2-w3 weight"

***person-level longitudinal weight***
*respondent longitudinal weight, combined sample
gen ral23wtrsp =.
replace ral23wtrsp = cliw_c
label variable ral23wtrsp "ral23wtrsp:w`wv' person-level longitudinal w2-w3 weight"

***birth date***
*respondent birth year (update)
missing_common yrbirth  if missing(rabyear), result(rabyear)
replace rabyear = yrbirth if inrange(yrbirth,1890,2020) & missing(rabyear)

*wave 3 respondent birth month
gen r3bmonth =.
missing_common mobirth, result(r3bmonth)
replace r3bmonth = mobirth if inrange(mobirth,1,12)

***death date***
*respondent death year (update)
missing_common deceased_year if mi(radyear), result(radyear)
replace radyear = .i if deceased_year == -3 & mi(radyear)
replace radyear = deceased_year if inrange(deceased_year,2000,2020) & mi(radyear)

*respondent death month (update)
missing_common deceased_month if mi(radmonth), result(radmonth)
replace radmonth = .i if deceased_month == -3 & mi(radmonth)
replace radmonth = deceased_month if inrange(deceased_month,1,12) & mi(radmonth)

***gender***
*respondent gender (update)
missing_common gender if missing(ragender), result(ragender)
replace ragender = 1 if gender == 1 & missing(ragender)
replace ragender = 2 if gender == 2 & missing(ragender)

***Language of Interview***
*wave 3 respondent language of interview
gen r`wv'lang_s = .
replace r`wv'lang_s = 1 if inlist(language,11,12,20,42) & inw`wv'==1
replace r`wv'lang_s = 2 if language == 13 & inw`wv'==1
replace r`wv'lang_s = 3 if language == 14 & inw`wv'==1
replace r`wv'lang_s = 4 if inlist(language,15,40) & inw`wv'==1
replace r`wv'lang_s = 5 if inlist(language,16,22) & inw`wv'==1
replace r`wv'lang_s = 6 if inlist(language,17,21,23,41) & inw`wv'==1
replace r`wv'lang_s = 7 if language == 18 & inw`wv'==1
replace r`wv'lang_s = 8 if language == 19 & inw`wv'==1
replace r`wv'lang_s = 9 if language == 24 & inw`wv'==1
replace r`wv'lang_s = 10 if language == 25 & inw`wv'==1
replace r`wv'lang_s = 11 if language == 26 & inw`wv'==1
replace r`wv'lang_s = 12 if inlist(language,27,37) & inw`wv'==1
replace r`wv'lang_s = 13 if language == 28 & inw`wv'==1
replace r`wv'lang_s = 14 if language == 29 & inw`wv'==1
replace r`wv'lang_s = 15 if language == 30 & inw`wv'==1
replace r`wv'lang_s = 16 if language == 32 & inw`wv'==1
replace r`wv'lang_s = 17 if inlist(language,33,43) & inw`wv'==1
replace r`wv'lang_s = 18 if language == 34 & inw`wv'==1
replace r`wv'lang_s = 19 if language == 36 & inw`wv'==1
replace r`wv'lang_s = 20 if language == 39 & inw`wv'==1
replace r`wv'lang_s = 21 if language == 47 & inw`wv'==1
replace r`wv'lang_s = 22 if language == 31 & inw`wv'==1
replace r`wv'lang_s = 23 if language == 35 & inw`wv'==1
replace r`wv'lang_s = 24 if language == 38 & inw`wv'==1
replace r`wv'lang_s = 25 if language == 44 & inw`wv'==1
replace r`wv'lang_s = 26 if language == 45 & inw`wv'==1
label variable r`wv'lang_s "r`wv'lang_s:w`wv' r language of interview"
label values r`wv'lang_s languages

*wave 3 spouse language of interview
gen s`wv'lang_s = .
spouse r`wv'lang_s, result(s`wv'lang_s) wave(`wv')
label variable s`wv'lang_s "s`wv'lang_s:w`wv' s language of interview"
label values s`wv'lang_s languages



***drop cover screen tracker (respondent) file raw variables
drop `demo_all_cv_r'


***drop wave 3 Generated Variables (Weights) file raw variables
drop `demo_w3_gv_weights'

***drop Longititudinal Weights wave 2 wave 3 file raw variables
drop `demo_long_weight_w2w3'


******************************************************************************************

*set wave number
local wv = 4

***merge with cover screen tracker (respondent) data***
local demo_all_cv_r mergeidp4 interview_w4 int_year_w4 int_month_w4 interview_hh_w4 ///
                fam_resp_w4 fin_resp_w4 hou_resp_w4 partnerinhh_w4 coupleid4 ///
                gender yrbirth mobirth country waveid_hh firstwave_hh ///
                deadoralive_w3 deadoralive_w4 deceased_year deceased_month deceased_age agep2011
merge 1:1 mergeid using "$tracker", keepusing(`demo_all_cv_r' hhid4 age_int_w4) nogen

***merge with wave 4 Asset data***
local demo_w4_as as057_
merge 1:1 mergeid using "$wave_4_as", keepusing(`demo_w4_as') nogen

***merge with wave 4 Behavioural Risk data***
local demo_w4_br br017_
merge 1:1 mergeid using "$wave_4_br", keepusing(`demo_w4_br') nogen

***merge with wave 4 Children data***
local demo_w4_ch ch023_
merge 1:1 mergeid using "$wave_4_ch", keepusing(`demo_w4_ch') nogen

***merge with wave 4 Consumption data***
local demo_w4_co co009_
merge 1:1 mergeid using "$wave_4_co", keepusing(`demo_w4_co') nogen

***merge with wave 4 Demographic data***
local demo_w4_dn dn041_ dn014_ dn044_ dn015_ dn017_ dn004_ dn005c language dn002_ dn042_
merge 1:1 mergeid using "$wave_4_dn", keepusing(`demo_w4_dn') nogen

***merge with wave 4 Employment and Pension data***
local demo_w4_ep ep210_
merge 1:1 mergeid using "$wave_4_ep", keepusing(`demo_w4_ep') nogen

***merge with wave 4 Financial Transfer data***
local demo_w4_ft ft021_
merge 1:1 mergeid using "$wave_4_ft", keepusing(`demo_w4_ft') nogen

***merge with wave 4 Household Income data***
local demo_w4_hh hh014_
merge 1:1 mergeid using "$wave_4_hh", keepusing(`demo_w4_hh') nogen

***merge with wave 4 Housing data***
local demo_w4_ho ho041_
merge 1:1 mergeid using "$wave_4_ho", keepusing(`demo_w4_ho') nogen

***merge with wave 4 interviewer data***
local demo_w4_iv iv009_
merge 1:1 mergeid using "$wave_4_iv", keepusing(`demo_w4_iv') nogen

***merge with wave 4 Physical Health data***
local demo_w4_ph ph054_
merge 1:1 mergeid using "$wave_4_ph", keepusing(`demo_w4_ph') nogen

***merge with wave 4 Social Network data***
local demo_w4_sn sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7 sn005a_?
merge 1:1 mergeid using "$wave_4_sn", keepusing(`demo_w4_sn') nogen

***merge with wave 4 Social Support data***
local demo_w4_sp sp022_
merge 1:1 mergeid using "$wave_4_sp", keepusing(`demo_w4_sp') nogen

***merge with wave 4 Dropoff data***
local demo_w4_dropoff q1a
merge 1:1 mergeid using "$wave_4_dropoff", keepusing(`demo_w4_dropoff') nogen

***merge with wave 4 Generated Variables (Weights) data***
local demo_w4_gv_weights cchw_w4 cciw_w4 dw_w4 ///
                         psu ssu stratum1 stratum2
merge 1:1 mergeid using "$wave_4_gv_weights", keepusing(`demo_w4_gv_weights') nogen

***merge with wave 4 Generated Variables (Netwowrk) data***
local demo_w4_gv_network parentnet
merge 1:1 mergeid using "$wave_4_gv_network", keepusing(`demo_w4_gv_network') nogen

***merge with wave 4 Generated Variables (Imputations) data***
local demo_w4_imputations isced isced_f yedu yedu_f implicat p_nrp
merge 1:1 mergeid using "$wave_4_imputations_", keepusing(`demo_w4_imputations') nogen

***merge with Longititudinal Weights wave 3 wave 4 data***
local demo_long_weight_w3w4 clhw_d cliw_d
merge 1:1 mergeid using "$long_weight_w3w4", keepusing(`demo_long_weight_w3w4') nogen

***merge with wave 1 Generated Variables ISCED data***
local demo_w4_isced isced1997_r 
merge 1:1 mergeid using "$wave_4_gv_isced", keepusing(`demo_w4_isced') nogen


***wave status: response indicator***
*wave 1 response indicator (update)
replace inw1 = 0 if missing(inw1)

*wave 2 response indicator (update)
replace inw2 = 0 if missing(inw2)

*wave 3 response indicator (update)
replace inw3 = 0 if missing(inw3)

*wave 4 response indicator
gen inw4 = 0
replace inw4 = 1 if interview_w4 == 1
label variable inw4 "inw4: =1 if respondent w4"
label values inw4 inw

***wave status: drop-off response indicator***
*wave 4 response indicator
gen inw4sc = 0
replace inw4sc = 1 if !mi(q1a)
label variable inw4sc "inw4sc: =1 if respondent w4 drop-off"
label values inw4sc inw

***person specific identifier***
*country id (char)
replace cid = substr(mergeid,1,2) if missing(cid)
replace cid = "11" if cid == "AT"
replace cid = "12" if cid == "DE"
replace cid = "13" if cid == "SE"
replace cid = "14" if cid == "NL"
replace cid = "15" if cid == "ES" | cid == "Eg"
replace cid = "16" if cid == "IT"
replace cid = "17" if cid == "FR" | cid == "F1"
replace cid = "18" if cid == "DK"
replace cid = "19" if cid == "GR"
replace cid = "20" if cid == "Cg"
replace cid = "21" if cid == "Cf"
replace cid = "22" if cid == "Ci"
replace cid = "23" if cid == "Bf"
replace cid = "24" if cid == "Bn"
replace cid = "25" if cid == "Ih"
replace cid = "26" if cid == "Ia"
replace cid = "27" if cid == "Ir"
replace cid = "28" if cid == "CZ"
replace cid = "29" if cid == "PL"
replace cid = "30" if cid == "IE"
replace cid = "31" if cid == "LU"
replace cid = "32" if cid == "HU"
replace cid = "33" if cid == "PT"
replace cid = "34" if cid == "SI"
replace cid = "35" if cid == "EE"
replace cid = "47" if cid == "HR"

*household id (char) (update)
replace hhidc = substr(mergeid,4,6) if missing(hhidc)

*person number (char) (update)
replace pn = substr(mergeid,11,2) if missing(pn)
*note: pn = a5, a6, b5, b6 are coverscreen respondents outside of the household
*we convert them to numbers to create a numerical version of nmergeid
replace pn = "90" if pn == "a5"
replace pn = "91" if pn == "a6"
replace pn = "92" if pn == "b5"
replace pn = "93" if pn == "b6"

*merge identifier (num) (update)
tempvar w4mergeid
gen `w4mergeid' =cid+hhidc+pn
destring `w4mergeid', replace
replace nmergeid = `w4mergeid' if missing(nmergeid)

***household identifier***
*wave 4 household id (char)
tempvar subhh hhidc
gen `subhh' = substr(hhid4,11,1)
replace `subhh' = "1" if `subhh'=="A"
replace `subhh' = "2" if `subhh'=="B"
gen `hhidc' = substr(hhid4,4,6)
gen hh4hhidc =""
replace hh4hhidc = cid + `hhidc' + `subhh' if inw4 == 1
label variable hh4hhidc "hh4hhidc:w4 cid + hhold id + subhousehold /9-char"

*wave 4 household id (num)
destring hh4hhidc, generate(hh4hhid)
label variable hh4hhid "hh4hhid:w4 cid + hhold id + subhousehold /Num"
format hh4hhid %9.0f
label variable hhid4 "hhid4: SHARE household identifier wave 4"

***Country*** (update)
replace racountry = country if mi(racountry)

replace isocountry = 040 if country == 11
replace isocountry = 276 if country == 12
replace isocountry = 752 if country == 13
replace isocountry = 528 if country == 14
replace isocountry = 724 if country == 15
replace isocountry = 380 if country == 16
replace isocountry = 250 if country == 17
replace isocountry = 208 if country == 18
replace isocountry = 300 if country == 19
replace isocountry = 756 if country == 20
replace isocountry = 056 if country == 23
replace isocountry = 376 if country == 25
replace isocountry = 203 if country == 28
replace isocountry = 616 if country == 29
replace isocountry = 372 if country == 30
replace isocountry = 348 if country == 32
replace isocountry = 620 if country == 33
replace isocountry = 705 if country == 34
replace isocountry = 233 if country == 35
replace isocountry = 191 if country == 47



***Implicate***
replace implicate = implicat if mi(implicate)

***couple identifier***
*wave 4 couple id (number)
gen pnp = substr(mergeidp4,11,2)

gen hh_coup=""
levelsof pn if !mi(hhid4), local(pns)
foreach r of local pns {
	forvalues i = 1/9 {
		bysort hhid4: replace hh_coup = "`r'"+"`i'" ///
			if pn == pnp[_n-`i'] & pn == "`r'"
		bysort hhid4: replace hh_coup ="`r'"+"`i'" ///
			if hh_coup[_n+`i'] == "`r'"+"`i'"
	}
}
replace hh_coup=string(_n) if missing(hh_coup)
egen h`wv'coupid = group(hhid4 hh_coup) if !mi(hhid4)
label variable h`wv'coupid "wave `wv' specific couple id number /num"
drop hh_coup pnp

***Spouse Identifier***
*wave 4 spouse id (char)
gen s4mergeid = "0" if inw4 == 1
bysort h4coupid: replace s4mergeid = mergeid[_n+1] if !missing(mergeid[_n+1]) & !missing(h4coupid)
bysort h4coupid: replace s4mergeid = mergeid[_n-1] if !missing(mergeid[_n-1]) & !missing(h4coupid)
label variable s4mergeid "s4mergeid:w4 spouse mergeid /12-char"

*wave 4 spouse id (num)
gen s4nmergeid =""
replace s4nmergeid = "0"  if s4mergeid=="0"

tempvar s4_cid s4_hhidc s4_pn

gen `s4_cid' = substr(s4mergeid,1,2)
replace `s4_cid' = "11" if `s4_cid' == "AT"
replace `s4_cid' = "12" if `s4_cid' == "DE"
replace `s4_cid' = "13" if `s4_cid' == "SE"
replace `s4_cid' = "14" if `s4_cid' == "NL"
replace `s4_cid' = "15" if `s4_cid' == "ES" | `s4_cid' == "Eg"
replace `s4_cid' = "16" if `s4_cid' == "IT"
replace `s4_cid' = "17" if `s4_cid' == "FR" | `s4_cid' == "F1"
replace `s4_cid' = "18" if `s4_cid' == "DK"
replace `s4_cid' = "19" if `s4_cid' == "GR"
replace `s4_cid' = "20" if `s4_cid' == "Cg"
replace `s4_cid' = "21" if `s4_cid' == "Cf"
replace `s4_cid' = "22" if `s4_cid' == "Ci"
replace `s4_cid' = "23" if `s4_cid' == "Bf"
replace `s4_cid' = "24" if `s4_cid' == "Bn"
replace `s4_cid' = "25" if `s4_cid' == "Ih"
replace `s4_cid' = "26" if `s4_cid' == "Ia"
replace `s4_cid' = "27" if `s4_cid' == "Ir"
replace `s4_cid' = "28" if `s4_cid' == "CZ"
replace `s4_cid' = "29" if `s4_cid' == "PL"
replace `s4_cid' = "30" if `s4_cid' == "IE"
replace `s4_cid' = "31" if `s4_cid' == "LU"
replace `s4_cid' = "32" if `s4_cid' == "HU"
replace `s4_cid' = "33" if `s4_cid' == "PT"
replace `s4_cid' = "34" if `s4_cid' == "SI"
replace `s4_cid' = "35" if `s4_cid' == "EE"

gen `s4_hhidc' = substr(s4mergeid,4,6)

gen `s4_pn' = substr(s4mergeid,11,2)

replace s4nmergeid =`s4_cid'+`s4_hhidc'+`s4_pn' if missing(s4nmergeid)
label variable s4nmergeid "s4nmergeid:w4 spouse mergeid /num"

*respondent number of spouse id's (update)
replace raspct = raspct + 1 if s4nmergeid != s1nmergeid & s4nmergeid != s2nmergeid & s4nmergeid != s3nmergeid & !inlist(s4nmergeid,"0","")
replace raspct = 0 if s4nmergeid == "0" & missing(raspct)
replace raspct = 1 if !inlist(s4nmergeid,"0","") & missing(raspct)

*respondent first spouse id (update)
replace raspid1 = s4nmergeid if !inlist(s4nmergeid,"0","") & missing(raspid1)
codebook raspid1

*respondent second spouse id (update)
replace raspid2 = s4nmergeid if s4nmergeid != raspid1 & !inlist(raspid1,"0","") & ///
                                !inlist(s4nmergeid,"0","") & mi(raspid2)
codebook raspid2

*respondent third spouse id
replace raspid3 = s4nmergeid if s4nmergeid != raspid1 & !inlist(raspid1,"0","") & ///
                                s4nmergeid != raspid2 & !inlist(raspid2,"0","") & /// 
                                !inlist(s4nmergeid,"0","") & mi(raspid3)
codebook raspid3

*respondent fourth spouse id
gen raspid4 =""
replace raspid4 = s4nmergeid if s4nmergeid != raspid1 & !inlist(raspid1,"0","") & ///
                                s4nmergeid != raspid2 & !inlist(raspid2,"0","") & ///
                                s4nmergeid != raspid3 & !inlist(raspid3,"0","") & /// 
                                !inlist(s4nmergeid,"0","") & mi(raspid4)
label variable raspid4 "raspid4: mergeid of 4th spouse"
codebook raspid4

*create temporary indicator of wave 4 spouse
gen w`wv'spouse=0 if missing(mergeidp4) & inw`wv'==1
bysort h`wv'coupid: replace w`wv'spouse = 1 if !mi(mergeidp4) & inw`wv'[_n-1] == 1 & inw`wv' == 1 & !mi(h`wv'coupid)
bysort h`wv'coupid: replace w`wv'spouse = 1 if !mi(mergeidp4) & inw`wv'[_n+1] == 1 & inw`wv' == 1 & !mi(h`wv'coupid)
bysort h`wv'coupid: replace w`wv'spouse = 2 if !mi(mergeidp4) & inw`wv'[_n-1] == 0 & inw`wv' == 1 & !mi(h`wv'coupid)
bysort h`wv'coupid: replace w`wv'spouse = 2 if !mi(mergeidp4) & inw`wv'[_n+1] == 0 & inw`wv' == 1 & !mi(h`wv'coupid)

***wave status: interview status***
*wave 1 interview status (update)
replace r1iwstat = 0 if missing(r1iwstat)

*wave 2 interview status (update)
replace r2iwstat = 0 if missing(r2iwstat)

*wave 3 interview status (update)
replace r3iwstat = 0 if missing(r3iwstat)

*wave 4 interview status
gen r4iwstat = .
replace r4iwstat = 0
replace r4iwstat = 1 if inw4 == 1
replace r4iwstat = 4 if inw4 == 0 & deadoralive_w4 == 1
replace r4iwstat = 5 if inw4 == 0 & deadoralive_w3 == 1 & deadoralive_w4 == 2
replace r4iwstat = 6 if inw4 == 0 & deadoralive_w3 == 2 & deadoralive_w4 == 2
replace r4iwstat = 9 if inw4 == 0 & deadoralive_w4 == 0
label variable r4iwstat "r4iwstat:w4 r interview status"
label values r4iwstat wstat

*wave 4 spouse interview status
gen s4iwstat = .
spouse r4iwstat, result(s4iwstat) wave(4)
label variable s4iwstat "s4iwstat:w4 s interview status"
label values s4iwstat wstat

***cohort***
*sample cohort (update)
replace hacohort = 1 if waveid_hh == 102 & inlist(country,32,33,34,35) & mi(hacohort)
replace hacohort = 3 if waveid_hh == 102 & inlist(country,11,14,15,16,17,18,20,23,28) & mi(hacohort)

****Wave in which the household was sampled***
*household wave in which sampled (update)
replace hawvsamp = firstwave_hh if missing(hawvsamp)

***sampling weight***
*sampling weight, combined sample
gen r`wv'wtsamp =.
replace r`wv'wtsamp = dw_w4
label variable r`wv'wtsamp "r`wv'wtsamp:w`wv' design weight"

*respondent psu (update)
replace rapsu1 = real(psu) if mi(rapsu1)

*respondent psu 2 (update)
replace rapsu2 = real(ssu) if mi(rapsu2)

*respondent stratification variable (update)
replace raestrat1 = stratum1 if mi(stratum1)

*respondent stratification variable 2 (update)
replace raestrat2 = stratum2 if mi(raestrat2)

***Household analysis weight***
*wave 4 household analysis weight, combined sample
gen r`wv'wthh =.
replace r`wv'wthh = cchw_w4
label variable r`wv'wthh "r`wv'wthh:w`wv' household analysis weight"

***person-level analysis weight***
*wave 4 respondent analysis weight, combined sample
gen r`wv'wtresp =.
replace r`wv'wtresp = cciw_w4
label variable r`wv'wtresp "r`wv'wtresp:w`wv' person-level analysis weight"

***household longitudinal weight***
*household longitudinal weight, combined sample
gen ral34wthh =.
replace ral34wthh = clhw_d
label variable ral34wthh "ral34wthh:w`wv' household longitudinal w3-w4 weight"

***person-level longitudinal weight***
*respondent longitudinal weight, combined sample
gen ral34wtrsp =.
replace ral34wtrsp = cliw_d
label variable ral34wtrsp "ral34wtrsp:w`wv' person-level longitudinal w3-w4 weight"

***number of household respondents***     
*wave 4 household number of respondents 
egen hh4hhresp = count(inw4==1) if inw4==1, by(hh4hhid)
label variable hh4hhresp "hh4hhresp:w4 # core respondents in hh"

***whether coupled***
*wave 4 couple indicator
egen h4cpl = sum(inw4==1) if inw4==1, by(h4coupid)
recode h4cpl (0= .) (1=0) (2=1)
label variable h4cpl "h4cpl:w4 whether coupled"
label values h4cpl cpl

***financial respondent***
*wave 1 respondent financial respondent indicator
gen r`wv'finr =.
replace r`wv'finr = 0 if fin_resp_w4 == 0
replace r`wv'finr = 1 if fin_resp_w4 == 1
label variable r`wv'finr "r`wv'finr:w`wv' whether r financial resp"
label values r`wv'finr finr

*wave 1 spouse financial respondent indicator
gen s`wv'finr =.
spouse r`wv'finr, result(s`wv'finr) wave(`wv')
label variable s`wv'finr "s`wv'finr:w`wv' whether s financial resp"
label values s`wv'finr  finr

***hosuehold respondent***
*wave 1 respondent household respondent indicator
gen r`wv'hhr =.
replace r`wv'hhr = 1 if hou_resp_w4 == 1
replace r`wv'hhr = 0 if hou_resp_w4 == 0
label variable r`wv'hhr "r`wv'hhr:w`wv' whether r hh resp"
label values r`wv'hhr hhr

*wave 1 spouse household respondent indicator
gen s`wv'hhr =.
spouse r`wv'hhr, result(s`wv'hhr) wave(`wv')
label variable s`wv'hhr "s`wv'hhr:w`wv' whether s hh resp"
label values s`wv'hhr hhr

***family respondent***
*wave 1 respondent family respondent indicator
gen r`wv'famr =.
replace r`wv'famr = 1 if fam_resp_w4 == 1
replace r`wv'famr = 0 if fam_resp_w4 == 0
label variable r`wv'famr "r`wv'famr:w`wv' whether r family resp"
label values r`wv'famr   famr

*wave 1 spouse family respondent indicator
gen s`wv'famr =.
spouse r`wv'famr, result(s`wv'famr) wave(`wv')
label variable s`wv'famr "s`wv'famr:w`wv' whether s family resp"
label values s`wv'famr  famr

***whether proxy interview***
*wave 4 respondent proxy indicator
gen r4proxy = .
missing_common ph054 br017 ep210 ch023 sp022 ft021 ho041 hh014 co009 as057, result(r4proxy)
replace r4proxy = 0 if ph054 ==1 ///
					 & br017 ==1 ///
					 & ep210 ==1 ///
					 & ch023 ==1 ///
					 & sp022 ==1 ///
					 & ft021 ==1 ///
					 & ho041 ==1 ///
					 & hh014 ==1 ///
					 & co009 ==1 ///
					 & as057 ==1
replace r4proxy = 1 if ( inlist(ph054,1,2,3) ///
    					 | inlist(br017,1,2,3) ///
    					 | inlist(ep210,1,2,3) ///
    					 | inlist(ch023,1,2,3) ///
    					 | inlist(sp022,1,2,3) ///
    					 | inlist(ft021,1,2,3) ///
    					 | inlist(ho041,1,2,3) ///
    					 | inlist(hh014,1,2,3) ///
    					 | inlist(co009,1,2,3) ///
    					 | inlist(as057,1,2,3) ) & missing(r4proxy)
replace r4proxy = 2 if ph054 ==3 ///
					 & br017 ==3 ///
					 & ep210 ==3 ///
					 & ch023 ==3 ///
					 & sp022 ==3 ///
					 & ft021 ==3 ///
					 & ho041 ==3 ///
					 & hh014 ==3 ///
					 & co009 ==3 ///
					 & as057 ==3
label variable r4proxy "r4proxy:w4 whether r proxy interview"

*wave 4 spouse proxy indicator
gen s4proxy = .
spouse r4proxy, result(s4proxy) wave(4)
label variable s4proxy "s4proxy:w4 whether s proxy interview"
label values r4proxy proxy
label values s4proxy proxy

***interview date***
*wave 4 respondent household interview month
gen r4iwm = .
missing_common int_month_w4 if interview_w4 == 1, result(r4iwm)
replace r4iwm = int_month_w4 if inrange(int_month_w4,1,12) & interview_w4 == 1
label variable r4iwm "r4iwm:w4 household interview month"

*wave 4 respondent household interview year
gen r4iwy = .
missing_common int_year_w4 if interview_w4 == 1, result(r4iwy)
replace r4iwy = int_year_w4 if inrange(int_year_w4,2000, 2020) & interview_w4 == 1
label variable r4iwy "r4iwy:w4 household interview year"

*wave 4 spouse household interview month
gen s4iwm = .
spouse r4iwm, result(s4iwm) wave(4)
label variable s4iwm "s4iwm:w4 household interview month"

*wave 4 spouse household interview year
gen s4iwy = .
spouse r4iwy, result(s4iwy) wave(4)
label variable s4iwy "s4iwy:w4 household interview year"

***birth date***
*respondent birth year (update)
missing_common yrbirth  if missing(rabyear), result(rabyear)
replace rabyear = yrbirth if inrange(yrbirth,1890,2020) & missing(rabyear)

*wave 4 spouse birth year
gen s4byear = .
spouse rabyear, result(s4byear) wave(4)
label variable s4byear "s4byear: s birth year"

*wave 4 respondent birth month
gen r4bmonth =.
missing_common dn002_, result(r4bmonth)
replace r4bmonth = dn002_ if inrange(dn002_,1,12)

***death date***
*respondent death year (update)
missing_common deceased_year if mi(radyear), result(radyear)
replace radyear = .i if deceased_year == -3 & mi(radyear)
replace radyear = deceased_year if inrange(deceased_year,2000,2020) & mi(radyear)

*respondent death month (update)
missing_common deceased_month if mi(radmonth), result(radmonth)
replace radmonth = .i if deceased_month == -3 & mi(radmonth)
replace radmonth = deceased_month if inrange(deceased_month,1,12) & mi(radmonth)

***SHARE sampling eligibility***
*wave 4 spouse sampling eligibility
gen s4sampelig= .
spouse rasampelig, result(s4sampelig) wave(4)
label variable s4sampelig "s4sampelig:w4 s eligibility at w1 sampling"
label values s4sampelig share_status

****gender***
**respondent gender (update)
*missing_common gender if missing(ragender), result(ragender)
*replace ragender = 1 if gender == 1 & missing(ragender)
*replace ragender = 2 if gender == 2 & missing(ragender)
*
**wave 4 spouse gender
*gen s4gender = .
*spouse ragender, result(s4gender) wave(4)
*label variable s4gender "s4gender:w`wv' s gender"
*label values s4gender gender

***gender***
*respondent gender (update)
missing_common dn042_ if missing(ragender), result(ragender)
replace ragender = 1 if dn042_ == 1 & missing(ragender)
replace ragender = 2 if dn042_ == 2 & missing(ragender)

*wave 4 spouse gender
gen s4gender = .
spouse ragender, result(s4gender) wave(4)
replace s4gender = .v if s`wv'iwstat==4
label variable s4gender "s4gender:w`wv' s gender"
label values s4gender gender

***education: years of education***
*respondent education in years (update)
replace raedyrs = round(yedu) if inrange(yedu,0,21) & mi(raedyrs)

*wave 4 spouse education in years
gen s`wv'edyrs =.
spouse raedyrs, result(s`wv'edyrs) wave(`wv')
label variable s`wv'edyrs "s`wv'edyrs:w`wv' s years of education"

*respondent years of education flag (update)
replace rafedyrs = 0 if yedu_f == 3 & mi(rafedyrs)
replace rafedyrs = 1 if yedu_f == 14 & mi(rafedyrs)

*wave 4 spouse education in years flag
gen s`wv'fedyrs =.
spouse rafedyrs, result(s`wv'fedyrs) wave(`wv')
label variable s`wv'fedyrs "s`wv'fedyrs:impflag w`wv' s years of education"
label values s`wv'fedyrs i_flag

***Education: ISCED code***
*respondent education by ISCED code (update)
replace raedisced = isced if inrange(isced,0,6) & mi(raedisced)

*wave 4 spouse education by ISCED code
gen s4edisced = .
spouse raedisced, result(s4edisced) wave(4)
label variable s4edisced "s4edisced:w4 s education by isced code"
label values s4edisced edisced

*respondent education by ISCED code flag (update)
replace rafedisced = 0 if isced_f == 3 & mi(rafedisced)
replace rafedisced = 1 if isced_f == 14 & mi(rafedisced)

*wave 4 spouse education by ISCED code
gen s4fedisced = .
spouse rafedisced, result(s4fedisced) wave(4)
label variable s4fedisced "s4fedisced:impflag w4 s education by isced code"
label values s4fedisced i_flag

****Education: ISCED code***ISCED file
**respondent education by ISCED code
*gen edisced`wv' = .
*replace edisced`wv'=.m if isced1997_r==. & inw`wv'==1
*replace edisced`wv'=.d if isced1997_r==-1
*replace edisced`wv'=.r if isced1997_r==-2
*replace edisced`wv'=.s if isced1997_r==95
*replace edisced`wv'=.o if isced1997_r==97
*replace edisced`wv' = isced1997_r if inrange(isced1997_r,0,6)

***Education, harmonized***
*respondent education harmonized (update)
replace raeducl = 1 if inrange(isced,0,2) & mi(raeducl)
replace raeducl = 2 if inrange(isced,3,4) & mi(raeducl)
replace raeducl = 3 if inrange(isced,5,6) & mi(raeducl)

*wave 4 spouse education harmonized
gen s4educl =.
spouse raeducl, result(s4educl) wave(4)
label variable s4educl "s4educl:w4 s harmonized education level"
label values s4educl edharm

*respondent education harmonized flag (update)
replace rafeducl = 0 if isced_f == 3 & mi(rafeducl)
replace rafeducl = 1 if isced_f == 14 & mi(rafeducl)

*wave 4 spouse education harmonized
gen s4feducl =.
spouse rafeducl, result(s4feducl) wave(4)
label variable s4feducl "s4feducl:impflag w4 s harmonized education level"
label values s4feducl i_flag

***current marital status: with partnerships***
*wave 4 respondent marital status with partnership
gen r4mstat= .
missing_common dn044_ dn014_, result(r4mstat)
replace r4mstat = r1mstat if r1mstat != . & dn044_ == 5
replace r4mstat = r2mstat if r2mstat != . & dn044_ == 5
replace r4mstat = 1 if dn014_ == 1
replace r4mstat = 4 if dn014_ == 3
replace r4mstat = 5 if dn014_ == 5
replace r4mstat = 7 if dn014_ == 6
replace r4mstat = 8 if dn014_ == 4
replace r4mstat = 3 if (dn014_ == 2) | (inlist(r4mstat,4,5,7,8) & inlist(w4spouse,1,2))
label variable r4mstat "r4mstat:w4 r marital status w/partners, filled"

*wave 4 spouse marital status with partnership
gen s4mstat= .
spouse r4mstat, result(s4mstat) wave(4)
label variable s4mstat "s4mstat:w4 s marital status w/partners, filled"
label values r4mstat marwpart
label values s4mstat marwpart

***current marital status: current implied partnership status***
*wave 4 respondent implied partnership status
gen r4mpart =0 if inw4==1
replace r4mpart = 1 if inlist(dn014_, 3, 4, 5, 6) & inlist(w4spouse,1,2)
label variable r4mpart "r4mpart:w4 r implied partnership status"

*wave 4 spouse implied partnership status
gen s4mpart = .
spouse r4mpart, result(s4mpart) wave(4)
label variable s4mpart "s4mpart:w4 s implied partnership status"
label values r4mpart part
label values s4mpart part

***current marital status: without implied partnerships***
*wave 4 respondent marital status without implied partnership
gen r4mstath = .
missing_common dn014_, result(r4mstath)
replace r4mstath = r1mstath if r1mstath != . & dn044_ == 5
replace r4mstath = r2mstath if r2mstath != . & dn044_ == 5
replace r4mstath = 1 if dn014_ == 1
replace r4mstath = 3 if dn014_ == 2
replace r4mstath = 4 if dn014_ == 3
replace r4mstath = 5 if dn014_ == 5
replace r4mstath = 7 if dn014_ == 6
replace r4mstath = 8 if dn014_ == 4
label variable r4mstath "r4mstath:w4 r marital status"
label values r4mstath  marwopart

*wave 4 spouse marital status without implied partnership
gen s4mstath = .
spouse r4mstath, result(s4mstath) wave(4)
label variable s4mstath "s4mstath:w4 s marital status"
label values s4mstath marwopart

***length of current marriage***
*wave 4 respondent length of current marriage
gen r4mcurln = .
missing_common dn015_ dn017_ int_year_w4, result(r4mcurln)
replace r4mcurln = int_year_w4 - dn015_ if inrange(dn015_,1900,2020) & inrange(int_year_w4,2000,2020)
replace r4mcurln = int_year_w4 - dn017_ if inrange(dn017_,1900,2020) & inrange(int_year_w4,2000,2020)
replace r4mcurln = .i if int_year_w4 < dn015_ & inrange(dn015_,1900,2020) & inrange(int_year_w4,2000,2020)
replace r4mcurln = .i if int_year_w4 < dn017_ & inrange(dn017_,1900,2020) & inrange(int_year_w4,2000,2020)
label variable r4mcurln "r4mcurln:w4 r length of current marriage"

*wave 4 spouse length of current marriage
gen s4mcurln = .
spouse r4mcurln, result(s4mcurln) wave(4)
label variable s4mcurln "s4mcurln:w4 s length of current marriage"

***place of birth***
*respondent place of birth (update)
missing_common dn004_ dn005c if missing(rabplace), result(rabplace)
replace rabplace = .n  if dn005c == -7 & missing(rabplace)
replace rabplace = 40  if country==11 & dn004_==1 & missing(rabplace)
replace rabplace = 276 if country==12 & dn004_==1 & missing(rabplace)
replace rabplace = 752 if country==13 & dn004_==1 & missing(rabplace)
replace rabplace = 528 if country==14 & dn004_==1 & missing(rabplace)
replace rabplace = 724 if country==15 & dn004_==1 & missing(rabplace)
replace rabplace = 380 if country==16 & dn004_==1 & missing(rabplace)
replace rabplace = 250 if country==17 & dn004_==1 & missing(rabplace)
replace rabplace = 208 if country==18 & dn004_==1 & missing(rabplace)
replace rabplace = 300 if country==19 & dn004_==1 & missing(rabplace)
replace rabplace = 756 if country==20 & dn004_==1 & missing(rabplace)
replace rabplace = 56  if country==23 & dn004_==1 & missing(rabplace)
replace rabplace = 376 if country==25 & dn004_==1 & missing(rabplace)
replace rabplace = 203 if country==28 & dn004_==1 & missing(rabplace)
replace rabplace = 616 if country==29 & dn004_==1 & missing(rabplace)
replace rabplace = 372 if country==30 & dn004_==1 & missing(rabplace)
replace rabplace = 348 if country==32 & dn004_==1 & missing(rabplace)
replace rabplace = 620 if country==33 & dn004_==1 & missing(rabplace)
replace rabplace = 705 if country==34 & dn004_==1 & missing(rabplace)
replace rabplace = 233 if country==35 & dn004_==1 & missing(rabplace)
replace rabplace = dn005c if inrange(dn005c,1,1500)  & missing(rabplace)

*wave 4 spouse place of birth
gen s4bplace = .
spouse rabplace, result(s4bplace) wave(4)
label variable s4bplace "s4bplace:w`wv' s place of birth"
label values s4bplace countries

***Whether Born in Country***
*respondent whether born in country
missing_common dn004_ if mi(rabcountry), result(rabcountry)
replace rabcountry = .m if dn004_ == . & inw`wv'==1 & !inlist(rabcountry,0,1)
replace rabcountry = 0 if dn004_ == 5 & !inlist(rabcountry,0,1)
replace rabcountry = 1 if dn004_ == 1 & !inlist(rabcountry,0,1)
replace rabcountry = 0 if !inlist(rabcountry,0,1) & isocountry!=rabplace & !mi(isocountry) & !mi(rabplace)
replace rabcountry = 1 if !inlist(rabcountry,0,1) & isocountry==rabplace & !mi(isocountry) & !mi(rabplace)
label variable rabcountry "rabcountry: r born in country of interview"
label values rabcountry bornin

*wave 4 spouse whether born in country
gen s`wv'bcountry = .
spouse rabcountry, result(s`wv'bcountry) wave(`wv')
label variable s`wv'bcountry "s`wv'bcountry:w`wv' s born in country of interview"
label values s`wv'bcountry bornin

***rural or urban***
*household wave 4 rural or urban
gen h`wv'rural = .
missing_common iv009_, result(h`wv'rural)
replace h`wv'rural = .m if iv009_ == . & inw`wv' == 1
replace h`wv'rural = 0 if inrange(iv009_,1,4)
replace h`wv'rural = 1 if iv009_ == 5
label variable h`wv'rural "h`wv'rural:w`wv' lives in rural or urban"
label values h`wv'rural rural

forvalues p = 1 / 9 {
    bysort hhid4: replace h`wv'rural = h`wv'rural[_n-`p'] if hhid4 == hhid4[_n-`p'] & mi(h`wv'rural) & h`wv'rural[_n-`p'] !=. & inw`wv' == 1 // copy values to other household memembers
    bysort hhid4: replace h`wv'rural = h`wv'rural[_n+`p'] if hhid4 == hhid4[_n+`p'] & mi(h`wv'rural) & h`wv'rural[_n+`p'] !=. & inw`wv' == 1 // copy values to other household memembers
}

***Language of Interview***
*wave 4 respondent language of interview
gen r`wv'lang_s = .
replace r`wv'lang_s = 1 if inlist(language,11,12,20,42)
replace r`wv'lang_s = 2 if language == 13
replace r`wv'lang_s = 3 if language == 14
replace r`wv'lang_s = 4 if inlist(language,15,40)
replace r`wv'lang_s = 5 if inlist(language,16,22)
replace r`wv'lang_s = 6 if inlist(language,17,21,23,41)
replace r`wv'lang_s = 7 if language == 18
replace r`wv'lang_s = 8 if language == 19
replace r`wv'lang_s = 9 if language == 24
replace r`wv'lang_s = 10 if language == 25
replace r`wv'lang_s = 11 if language == 26
replace r`wv'lang_s = 12 if inlist(language,27,37)
replace r`wv'lang_s = 13 if language == 28
replace r`wv'lang_s = 14 if language == 29
replace r`wv'lang_s = 15 if language == 30
replace r`wv'lang_s = 16 if language == 32
replace r`wv'lang_s = 17 if inlist(language,33,43)
replace r`wv'lang_s = 18 if language == 34
replace r`wv'lang_s = 19 if language == 36
replace r`wv'lang_s = 20 if language == 39
replace r`wv'lang_s = 21 if language == 47
replace r`wv'lang_s = 22 if language == 31
replace r`wv'lang_s = 23 if language == 35
replace r`wv'lang_s = 24 if language == 38
replace r`wv'lang_s = 25 if language == 44
replace r`wv'lang_s = 26 if language == 45
label variable r`wv'lang_s "r`wv'lang_s:w`wv' r language of interview"
label values r`wv'lang_s languages

*wave 4 spouse language of interview
gen s`wv'lang_s = .
spouse r`wv'lang_s, result(s`wv'lang_s) wave(`wv')
label variable s`wv'lang_s "s`wv'lang_s:w`wv' s language of interview"
label values s`wv'lang_s languages


***drop cover screen tracker (respondent) file raw variables
drop `demo_all_cv_r'

***drop wave 4 Asset file raw variables
drop `demo_w4_as'

***drop wave 4 Behavioural Risk file raw variables
drop `demo_w4_br'

***drop wave 4 Children file raw variables
drop `demo_w4_ch'

***drop wave 4 Consumption file raw variables
drop `demo_w4_co'

***drop wave 4 Demographic file raw variables
drop `demo_w4_dn'

***drop wave 4 Employment and Pension file raw variables
drop `demo_w4_ep'

***drop wave 4 Financial Transfer file raw variables
drop `demo_w4_ft'

***drop wave 4 Household Income file raw variables
drop `demo_w4_hh'

***drop wave 4 Housing file raw variables
drop `demo_w4_ho'

***drop wave 4 interviewer file raw variables
drop `demo_w4_iv'

***drop wave 4 Physical Health file raw variables
drop `demo_w4_ph'

***drop wave 4 Social Network file raw variables
drop `demo_w4_sn'

***drop wave 4 Social Support file raw variables
drop `demo_w4_sp'

***drop wave 4 Dropoff file raw variables
drop `demo_w4_dropoff'

***drop wave 4 Generated Variables (Weights) file raw variables
drop `demo_w4_gv_weights'

***drop Longititudinal Weights wave 3 wave 4 file raw variables
drop `demo_long_weight_w3w4'

***drop wave 4 Generated Variables (Netowrk) file raw variables
drop `demo_w4_gv_network'

***drop wave 4 Generated Variables (Imputations) file raw variables
drop `demo_w4_imputations'

***drop wave 1 Generated Variables (ISCED) file raw variables
drop `demo_w4_isced'

******************************************************************************************

*set wave number
local wv = 5

***merge with cover screen tracker (respondent) data***
local demo_all_cv_r mergeidp5 int_year_w5 int_month_w5 interview_hh_w5 ///
                fam_resp_w5 fin_resp_w5 hou_resp_w5 partnerinhh_w5 coupleid5 ///
                gender yrbirth mobirth country waveid_hh firstwave_hh ///
                deadoralive_w4 deadoralive_w5 deceased_year deceased_month deceased_age agep2013
merge 1:1 mergeid using "$tracker", keepusing(`demo_all_cv_r' hhid5 age_int_w5 interview_w5) nogen

***merge with wave 5 Asset data***
local demo_w5_as as057_
merge 1:1 mergeid using "$wave_5_as", keepusing(`demo_w5_as') nogen

***merge with wave 5 Behavioural Risk data***
local demo_w5_br br017_
merge 1:1 mergeid using "$wave_5_br", keepusing(`demo_w5_br') nogen

***merge with wave 5 Children data***
local demo_w5_ch ch023_
merge 1:1 mergeid using "$wave_5_ch", keepusing(`demo_w5_ch') nogen

***merge with wave 5 Consumption data***
local demo_w5_co co009_
merge 1:1 mergeid using "$wave_5_co", keepusing(`demo_w5_co') nogen

***merge with wave 5 Demographic data***
local demo_w5_dn dn041_ dn014_ dn044_ dn015_ dn017_ dn004_ dn005c language dn002_ dn042_
merge 1:1 mergeid using "$wave_5_dn", keepusing(`demo_w5_dn') nogen

***merge with wave 5 Employment and Pension data***
local demo_w5_ep ep210_
merge 1:1 mergeid using "$wave_5_ep", keepusing(`demo_w5_ep') nogen

***merge with wave 5 Financial Transfer data***
local demo_w5_ft ft021_
merge 1:1 mergeid using "$wave_5_ft", keepusing(`demo_w5_ft') nogen

***merge with wave 5 Household Income data***
local demo_w5_hh hh014_
merge 1:1 mergeid using "$wave_5_hh", keepusing(`demo_w5_hh') nogen

***merge with wave 5 Housing data***
local demo_w5_ho ho041_
merge 1:1 mergeid using "$wave_5_ho", keepusing(`demo_w5_ho') nogen

***merge with wave 5 interviewer data***
local demo_w5_iv iv009_
merge 1:1 mergeid using "$wave_5_iv", keepusing(`demo_w5_iv') nogen

***merge with wave 5 Physical Health data***
local demo_w5_ph ph054_
merge 1:1 mergeid using "$wave_5_ph", keepusing(`demo_w5_ph') nogen

***merge with wave 5 Social Support data***
local demo_w5_sp sp022_
merge 1:1 mergeid using "$wave_5_sp", keepusing(`demo_w5_sp') nogen

***merge with wave 5 Dropoff data***
local demo_w5_dropoff at_q1 cz_a_q1 il_q1_a
merge 1:1 mergeid using "$wave_5_dropoff", keepusing(`demo_w5_dropoff') nogen

***merge with wave 5 Generated Variables (Weights) data***
local demo_w5_gv_weights cchw_w5 cciw_w5 dw_w5 ///
                         psu ssu stratum1 stratum2
merge 1:1 mergeid using "$wave_5_gv_weights", keepusing(`demo_w5_gv_weights') nogen

***merge with wave 5 Generated Variables (Imputations) data***
local demo_w5_imputations isced isced_f yedu yedu_f implicat p_nrp
merge 1:1 mergeid using "$wave_5_imputations_", keepusing(`demo_w5_imputations') nogen

***merge with Longititudinal Weights wave 4 wave 5 data***
local demo_long_weight_w4w5 clhw_e cliw_e
merge 1:1 mergeid using "$long_weight_w4w5", keepusing(`demo_long_weight_w4w5') nogen

***merge with Longititudinal Weights wave 1 - wave 5 data***
*local demo_long_weight_w1w5 clhw_a cliw_a
*merge 1:1 mergeid using "$long_weight_w1w5", keepusing(`demo_long_weight_w1w5') nogen

***merge with wave 1 Generated Variables ISCED data***
local demo_w5_isced isced1997_r 
merge 1:1 mergeid using "$wave_5_gv_isced", keepusing(`demo_w5_isced') nogen


***wave status: response indicator***
*wave 1 response indicator (update)
replace inw1 = 0 if missing(inw1)

*wave 2 response indicator (update)
replace inw2 = 0 if missing(inw2)

*wave 3 response indicator (update)
replace inw3 = 0 if missing(inw4)

*wave 4 response indicator (update)
replace inw4 = 0 if missing(inw4)

*wave 5 response indicator
gen inw5 = 0
replace inw5 = 1 if interview_w5 == 1
label variable inw5 "inw5: =1 if respondent w5"
label values inw5 inw

***wave status: drop-off response indicator***
*wave 5 response indicator
gen inw5sc = 0
replace inw5sc = 1 if !mi(at_q1) | !mi(cz_a_q1) | !mi(il_q1_a)
label variable inw5sc "inw5sc: =1 if respondent w5 drop-off"
label values inw5sc inw

***person specific identifier***
*country id (char)
replace cid = substr(mergeid,1,2) if missing(cid)
replace cid = "11" if cid == "AT"
replace cid = "12" if cid == "DE"
replace cid = "13" if cid == "SE"
replace cid = "14" if cid == "NL"
replace cid = "15" if cid == "ES" | cid == "Eg"
replace cid = "16" if cid == "IT"
replace cid = "17" if cid == "FR" | cid == "F1"
replace cid = "18" if cid == "DK"
replace cid = "19" if cid == "GR"
replace cid = "20" if cid == "Cg"
replace cid = "21" if cid == "Cf"
replace cid = "22" if cid == "Ci"
replace cid = "23" if cid == "Bf"
replace cid = "24" if cid == "Bn"
replace cid = "25" if cid == "Ih"
replace cid = "26" if cid == "Ia"
replace cid = "27" if cid == "Ir"
replace cid = "28" if cid == "CZ"
replace cid = "29" if cid == "PL"
replace cid = "30" if cid == "IE"
replace cid = "31" if cid == "LU"
replace cid = "32" if cid == "HU"
replace cid = "33" if cid == "PT"
replace cid = "34" if cid == "SI"
replace cid = "35" if cid == "EE"
replace cid = "47" if cid == "HR"

*household id (char) (update)
replace hhidc = substr(mergeid,4,6) if missing(hhidc)

*person number (char) (update)
replace pn = substr(mergeid,11,2) if missing(pn)
*note: pn = a5, a6, b5, b6 are coverscreen respondents outside of the household
*we convert them to numbers to create a numerical version of nmergeid
replace pn = "90" if pn == "a5"
replace pn = "91" if pn == "a6"
replace pn = "92" if pn == "b5"
replace pn = "93" if pn == "b6"

*merge identifier (num) (update)
tempvar w5mergeid
gen `w5mergeid' =cid+hhidc+pn
destring `w5mergeid', replace
replace nmergeid = `w5mergeid' if missing(nmergeid)

***household identifier***
*wave 5 household id (char)
tempvar subhh hhidc
gen `subhh' = substr(hhid5,11,1)
replace `subhh' = "1" if `subhh'=="A"
replace `subhh' = "2" if `subhh'=="B"
gen `hhidc' = substr(hhid5,4,6)
gen hh5hhidc =""
replace hh5hhidc = cid + `hhidc' + `subhh' if inw5 == 1
label variable hh5hhidc "hh5hhidc:w5 cid + hhold id + subhousehold /9-char"

*wave 5 household id (num)
destring hh5hhidc, generate(hh5hhid)
label variable hh5hhid "hh5hhid:w5 cid + hhold id + subhousehold /Num"
format hh5hhid %9.0f
label variable hhid5 "hhid5: SHARE household identifier wave 5"

***Country*** (update)
replace racountry = country if mi(racountry)

replace isocountry = 040 if country == 11
replace isocountry = 276 if country == 12
replace isocountry = 752 if country == 13
replace isocountry = 528 if country == 14
replace isocountry = 724 if country == 15
replace isocountry = 380 if country == 16
replace isocountry = 250 if country == 17
replace isocountry = 208 if country == 18
replace isocountry = 300 if country == 19
replace isocountry = 756 if country == 20
replace isocountry = 056 if country == 23
replace isocountry = 376 if country == 25
replace isocountry = 203 if country == 28
replace isocountry = 616 if country == 29
replace isocountry = 372 if country == 30
replace isocountry = 442 if country == 31
replace isocountry = 348 if country == 32
replace isocountry = 620 if country == 33
replace isocountry = 705 if country == 34
replace isocountry = 233 if country == 35
replace isocountry = 191 if country == 47



***Implicate***
replace implicate = implicat if mi(implicate)

***couple identifier***
*wave 5 couple id (number)
gen pnp = substr(mergeidp5,11,2)

gen hh_coup=""
levelsof pn if !mi(hhid5), local(pns)
foreach r of local pns {
	forvalues i = 1/9 {
		bysort hhid5: replace hh_coup = "`r'"+"`i'" ///
			if pn == pnp[_n-`i'] & pn == "`r'"
		bysort hhid5: replace hh_coup ="`r'"+"`i'" ///
			if hh_coup[_n+`i'] == "`r'"+"`i'"
	}
}
replace hh_coup=string(_n) if missing(hh_coup)
egen h`wv'coupid = group(hhid5 hh_coup) if !mi(hhid5)
label variable h`wv'coupid "wave `wv' specific couple id number /num"
drop hh_coup pnp

***Spouse Identifier***
*wave 5 spouse id (char)
gen s5mergeid = "0" if inw5 == 1
bysort h5coupid: replace s5mergeid = mergeid[_n+1] if !missing(mergeid[_n+1]) & !missing(h5coupid)
bysort h5coupid: replace s5mergeid = mergeid[_n-1] if !missing(mergeid[_n-1]) & !missing(h5coupid)
label variable s5mergeid "s5mergeid:w5 spouse mergeid /12-char"

*wave 5 spouse id (num)
gen s5nmergeid =""
replace s5nmergeid = "0"  if s5mergeid=="0"

tempvar s5_cid s5_hhidc s5_pn

gen `s5_cid' = substr(s5mergeid,1,2)
replace `s5_cid' = "11" if `s5_cid' == "AT"
replace `s5_cid' = "12" if `s5_cid' == "DE"
replace `s5_cid' = "13" if `s5_cid' == "SE"
replace `s5_cid' = "14" if `s5_cid' == "NL"
replace `s5_cid' = "15" if `s5_cid' == "ES" | `s5_cid' == "Eg"
replace `s5_cid' = "16" if `s5_cid' == "IT"
replace `s5_cid' = "17" if `s5_cid' == "FR" | `s5_cid' == "F1"
replace `s5_cid' = "18" if `s5_cid' == "DK"
replace `s5_cid' = "19" if `s5_cid' == "GR"
replace `s5_cid' = "20" if `s5_cid' == "Cg"
replace `s5_cid' = "21" if `s5_cid' == "Cf"
replace `s5_cid' = "22" if `s5_cid' == "Ci"
replace `s5_cid' = "23" if `s5_cid' == "Bf"
replace `s5_cid' = "24" if `s5_cid' == "Bn"
replace `s5_cid' = "25" if `s5_cid' == "Ih"
replace `s5_cid' = "26" if `s5_cid' == "Ia"
replace `s5_cid' = "27" if `s5_cid' == "Ir"
replace `s5_cid' = "28" if `s5_cid' == "CZ"
replace `s5_cid' = "29" if `s5_cid' == "PL"
replace `s5_cid' = "30" if `s5_cid' == "IE"
replace `s5_cid' = "31" if `s5_cid' == "LU"
replace `s5_cid' = "32" if `s5_cid' == "HU"
replace `s5_cid' = "33" if `s5_cid' == "PT"
replace `s5_cid' = "34" if `s5_cid' == "SI"
replace `s5_cid' = "35" if `s5_cid' == "EE"

gen `s5_hhidc' = substr(s5mergeid,4,6)

gen `s5_pn' = substr(s5mergeid,11,2)

replace s5nmergeid =`s5_cid'+`s5_hhidc'+`s5_pn' if missing(s5nmergeid)
label variable s5nmergeid "s5nmergeid:w5 spouse mergeid /num"
 
*respondent number of spouse id's (update)
replace raspct = raspct + 1 if s5nmergeid != s1nmergeid & s5nmergeid != s2nmergeid & s5nmergeid != s4nmergeid & !inlist(s5nmergeid,"0","")
replace raspct = 0 if s5nmergeid == "0" & missing(raspct)
replace raspct = 1 if !inlist(s5nmergeid,"0","") & missing(raspct)

*respondent first spouse id (update)
replace raspid1 = s5nmergeid if !inlist(s5nmergeid,"0","") & missing(raspid1)
codebook raspid1

*respondent second spouse id (update)
replace raspid2 = s5nmergeid if s5nmergeid != raspid1 & !inlist(raspid1,"0","") & ///
                                !inlist(s5nmergeid,"0","") & mi(raspid2)
codebook raspid2

*respondent third spouse id
replace raspid3 = s5nmergeid if s5nmergeid != raspid1 & !inlist(raspid1,"0","") & ///
                                s5nmergeid != raspid2 & !inlist(raspid2,"0","") & /// 
                                !inlist(s5nmergeid,"0","") & mi(raspid3)
codebook raspid3

*respondent fourth spouse id
replace raspid4 = s5nmergeid if s5nmergeid != raspid1 & !inlist(raspid1,"0","") & ///
								s5nmergeid != raspid2 & !inlist(raspid2,"0","") & ///
								s5nmergeid != raspid3 & !inlist(raspid3,"0","") & ///
								!inlist(s5nmergeid,"0","") & mi(raspid4)
codebook raspid4

*create temporary indicator of wave 5 spouse
gen w`wv'spouse=0 if missing(mergeidp5) & inw`wv'==1
bysort h`wv'coupid: replace w`wv'spouse = 1 if !mi(mergeidp5) & inw`wv'[_n-1] == 1 & inw`wv' == 1 & !mi(h`wv'coupid)
bysort h`wv'coupid: replace w`wv'spouse = 1 if !mi(mergeidp5) & inw`wv'[_n+1] == 1 & inw`wv' == 1 & !mi(h`wv'coupid)
bysort h`wv'coupid: replace w`wv'spouse = 2 if !mi(mergeidp5) & inw`wv'[_n-1] == 0 & inw`wv' == 1 & !mi(h`wv'coupid)
bysort h`wv'coupid: replace w`wv'spouse = 2 if !mi(mergeidp5) & inw`wv'[_n+1] == 0 & inw`wv' == 1 & !mi(h`wv'coupid)

***wave status: interview status***
*wave 1 interview status (update)
replace r1iwstat = 0 if missing(r1iwstat)

*wave 2 interview status (update)
replace r2iwstat = 0 if missing(r2iwstat)

*wave 3 interview status (update)
replace r3iwstat = 0 if missing(r3iwstat)

*wave 4 interview status (update)
replace r4iwstat = 0 if missing(r4iwstat)

*wave 5 interview status
gen r5iwstat = .
replace r5iwstat = 0
replace r5iwstat = 1 if inw5 == 1
replace r5iwstat = 4 if inw5 == 0 & deadoralive_w5 == 1
replace r5iwstat = 5 if inw5 == 0 & deadoralive_w4 == 1 & deadoralive_w5 == 2
replace r5iwstat = 6 if inw5 == 0 & deadoralive_w4 == 2 & deadoralive_w5 == 2
replace r5iwstat = 9 if inw5 == 0 & deadoralive_w5 == 0
label variable r5iwstat "r5iwstat:w5 r interview status"
label values r5iwstat wstat

*wave 5 spouse interview status
gen s5iwstat = .
spouse r5iwstat, result(s5iwstat) wave(5)
label variable s5iwstat "s5iwstat:w5 s interview status"
label values s5iwstat wstat

***cohort***
*sample cohort (update)
replace hacohort = 1 if waveid_hh == 122 & country == 31 & mi(hacohort)
replace hacohort = 4 if waveid_hh == 122 & inlist(country,12,13,14,15,16,18,23,25,28,34) & mi(hacohort)

****Wave in which the household was sampled***
*household wave in which sampled (update)
replace hawvsamp = firstwave_hh if missing(hawvsamp)

***sampling weight***
*sampling weight, combined sample
gen r`wv'wtsamp =.
replace r`wv'wtsamp = dw_w5
label variable r`wv'wtsamp "r`wv'wtsamp:w`wv' design weight"

*respondent psu (update)
replace rapsu1 = real(psu) if mi(rapsu1)

*respondent psu 2 (update)
replace rapsu2 = real(ssu) if mi(rapsu2)

*respondent stratification variable (update)
replace raestrat1 = stratum1 if mi(stratum1)

*respondent stratification variable 2 (update)
replace raestrat2 = stratum2 if mi(raestrat2)

***Household analysis weight***
*wave 5 household analysis weight, combined sample
gen r`wv'wthh =.
replace r`wv'wthh = cchw_w5
label variable r`wv'wthh "r`wv'wthh:w`wv' household analysis weight"

***person-level analysis weight***
*wave 5 respondent analysis weight, combined sample
gen r`wv'wtresp =.
replace r`wv'wtresp = cciw_w5
label variable r`wv'wtresp "r`wv'wtresp:w`wv' person-level analysis weight"

***household longitudinal weight***
*household longitudinal weight, combined sample
gen ral45wthh =.
replace ral45wthh = clhw_e
label variable ral45wthh "ral45wthh:w`wv' household longitudinal w4-w5 weight"

***person-level longitudinal weight***
*respondent longitudinal weight, combined sample
gen ral45wtrsp =.
replace ral45wtrsp = cliw_e
label variable ral45wtrsp "ral45wtrsp:w`wv' person-level longitudinal w4-w5 weight"


***household longitudinal weight***
*household longitudinal weight, combined sample
*gen ral15wthh =.
*replace ral15wthh = clhw_a
*label variable ral15wthh "ral15wthh:w`wv' household longitudinal w1-w5 weight"

***person-level longitudinal weight***
*respondent longitudinal weight, combined sample
*gen ral15wtrsp =.
*replace ral15wtrsp = cliw_a
*label variable ral15wtrsp "ral15wtrsp:w`wv' person-level longitudinal w1-w5 weight"

***number of household respondents***     
*wave 5 household number of respondents 
egen hh5hhresp = count(inw5==1) if inw5==1, by(hh5hhid)
label variable hh5hhresp "hh5hhresp:w5 # core respondents in hh"

***whether coupled***
*wave 5 couple indicator
egen h5cpl = sum(inw5==1) if inw5==1, by(h5coupid)
recode h5cpl (0= .) (1=0) (2=1)
label variable h5cpl "h5cpl:w5 whether coupled"
label values h5cpl cpl

***financial respondent***
*wave 5 respondent financial respondent indicator
gen r`wv'finr =.
replace r`wv'finr = 0 if fin_resp_w5 == 0
replace r`wv'finr = 1 if fin_resp_w5 == 1
label variable r`wv'finr "r`wv'finr:w`wv' whether r financial resp"
label values r`wv'finr finr

*wave 5 spouse financial respondent indicator
gen s`wv'finr =.
spouse r`wv'finr, result(s`wv'finr) wave(`wv')
label variable s`wv'finr "s`wv'finr:w`wv' whether s financial resp"
label values s`wv'finr  finr

***hosuehold respondent***
*wave 5 respondent household respondent indicator
gen r`wv'hhr =.
replace r`wv'hhr = 1 if hou_resp_w5 == 1
replace r`wv'hhr = 0 if hou_resp_w5 == 0
label variable r`wv'hhr "r`wv'hhr:w`wv' whether r hh resp"
label values r`wv'hhr hhr

*wave 5 spouse household respondent indicator
gen s`wv'hhr =.
spouse r`wv'hhr, result(s`wv'hhr) wave(`wv')
label variable s`wv'hhr "s`wv'hhr:w`wv' whether s hh resp"
label values s`wv'hhr hhr

***family respondent***
*wave 5 respondent family respondent indicator
gen r`wv'famr =.
replace r`wv'famr = 1 if fam_resp_w5 == 1
replace r`wv'famr = 0 if fam_resp_w5 == 0
label variable r`wv'famr "r`wv'famr:w`wv' whether r family resp"
label values r`wv'famr   famr

*wave 5 spouse family respondent indicator
gen s`wv'famr =.
spouse r`wv'famr, result(s`wv'famr) wave(`wv')
label variable s`wv'famr "s`wv'famr:w`wv' whether s family resp"
label values s`wv'famr  famr

***whether proxy interview***
*wave 5 respondent proxy indicator
gen r5proxy = .
missing_common ph054 br017 ep210 ch023 sp022 ft021 ho041 hh014 co009 as057, result(r5proxy)
replace r5proxy = 0 if ph054 ==1 ///
					 & br017 ==1 ///
					 & ep210 ==1 ///
					 & ch023 ==1 ///
					 & sp022 ==1 ///
					 & ft021 ==1 ///
					 & ho041 ==1 ///
					 & hh014 ==1 ///
					 & co009 ==1 ///
					 & as057 ==1
replace r5proxy = 1 if ( inlist(ph054,1,2,3) ///
    					 | inlist(br017,1,2,3) ///
    					 | inlist(ep210,1,2,3) ///
    					 | inlist(ch023,1,2,3) ///
    					 | inlist(sp022,1,2,3) ///
    					 | inlist(ft021,1,2,3) ///
    					 | inlist(ho041,1,2,3) ///
    					 | inlist(hh014,1,2,3) ///
    					 | inlist(co009,1,2,3) ///
    					 | inlist(as057,1,2,3) ) & missing(r5proxy)
replace r5proxy = 2 if ph054 ==3 ///
					 & br017 ==3 ///
					 & ep210 ==3 ///
					 & ch023 ==3 ///
					 & sp022 ==3 ///
					 & ft021 ==3 ///
					 & ho041 ==3 ///
					 & hh014 ==3 ///
					 & co009 ==3 ///
					 & as057 ==3
label variable r5proxy "r5proxy:w5 whether r proxy interview"

*wave 5 spouse proxy indicator
gen s5proxy = .
spouse r5proxy, result(s5proxy) wave(5)
label variable s5proxy "s5proxy:w5 whether s proxy interview"
label values r5proxy proxy
label values s5proxy proxy

***interview date***
*wave 5 respondent household interview month
gen r5iwm = .
missing_common int_month_w5 if interview_w5 == 1, result(r5iwm)
replace r5iwm = int_month_w5 if inrange(int_month_w5,1,12) & interview_w5 == 1
label variable r5iwm "r5iwm:w5 household interview month"

*wave 5 respondent household interview year
gen r5iwy = .
missing_common int_year_w5 if interview_w5 == 1, result(r5iwy)
replace r5iwy = int_year_w5 if inrange(int_year_w5,2000, 2020) & interview_w5 == 1
label variable r5iwy "r5iwy:w5 household interview year"

*wave 5 spouse household interview month
gen s5iwm = .
spouse r5iwm, result(s5iwm) wave(5)
label variable s5iwm "s5iwm:w5 household interview month"

*wave 5 spouse household interview year
gen s5iwy = .
spouse r5iwy, result(s5iwy) wave(5)
label variable s5iwy "s5iwy:w5 household interview year"

***birth date***
*respondent birth year (update)
missing_common yrbirth  if missing(rabyear), result(rabyear)
replace rabyear = yrbirth if inrange(yrbirth,1890,2020) & missing(rabyear)

*wave 5 spouse birth year
gen s5byear = .
spouse rabyear, result(s5byear) wave(5)
label variable s5byear "s5byear: s birth year"

*wave 5 respondent birth month
gen r5bmonth =.
missing_common dn002_, result(r5bmonth)
replace r5bmonth = dn002_ if inrange(dn002_,1,12)

***death date***
*respondent death year (update)
missing_common deceased_year if mi(radyear), result(radyear)
replace radyear = .i if deceased_year == -3 & mi(radyear)
replace radyear = deceased_year if inrange(deceased_year,2000,2020) & mi(radyear)

*respondent death month (update)
missing_common deceased_month if mi(radmonth), result(radmonth)
replace radmonth = .i if deceased_month == -3 & mi(radmonth)
replace radmonth = deceased_month if inrange(deceased_month,1,12) & mi(radmonth)

***SHARE sampling eligibility***
*wave 5 spouse sampling eligibility
gen s5sampelig= .
spouse rasampelig, result(s5sampelig) wave(5)
label variable s5sampelig "s5sampelig:w5 s eligibility at w1 sampling"
label values s5sampelig share_status

****gender***
*respondent gender (update)
*missing_common gender if missing(ragender), result(ragender)
*replace ragender = 1 if gender == 1 & missing(ragender)
*replace ragender = 2 if gender == 2 & missing(ragender)
*
**wave 5 spouse gender
*gen s5gender = .
*spouse ragender, result(s5gender) wave(5)
*label variable s5gender "s5gender:w`wv' s gender"
*label values s5gender gender

***gender***
*respondent gender (update)
missing_common dn042_ if missing(ragender), result(ragender)
replace ragender = 1 if dn042_ == 1 & missing(ragender)
replace ragender = 2 if dn042_ == 2 & missing(ragender)

*wave 5 spouse gender
gen s5gender = .
spouse ragender, result(s5gender) wave(5)
replace s5gender = .v if s`wv'iwstat==4
label variable s5gender "s5gender:w`wv' s gender"
label values s5gender gender

***education: years of education***
*respondent education in years (update)
replace raedyrs = round(yedu) if inrange(yedu,0,21) & mi(raedyrs)

*wave 5 spouse education in years
gen s`wv'edyrs =.
spouse raedyrs, result(s`wv'edyrs) wave(`wv')
label variable s`wv'edyrs "s`wv'edyrs:w`wv' s years of education"

*respondent years of education flag (update)
replace rafedyrs = 0 if yedu_f == 3 & mi(rafedyrs)
replace rafedyrs = 1 if yedu_f == 14 & mi(rafedyrs)

*wave 5 spouse education in years flag
gen s`wv'fedyrs =.
spouse rafedyrs, result(s`wv'fedyrs) wave(`wv')
label variable s`wv'fedyrs "s`wv'fedyrs:impflag w`wv' s years of education"
label values s`wv'fedyrs i_flag

***Education: ISCED code***
*respondent education by ISCED code (update)
replace raedisced = isced if inrange(isced,0,6) & mi(raedisced)

*wave 5 spouse education by ISCED code
gen s5edisced = .
spouse raedisced, result(s5edisced) wave(5)
label variable s5edisced "s5edisced:w5 s education by isced code"
label values s5edisced edisced

*respondent education by ISCED code flag (update)
replace rafedisced = 0 if isced_f == 3 & mi(rafedisced)
replace rafedisced = 1 if isced_f == 14 & mi(rafedisced)

*wave 5 spouse education by ISCED code
gen s5fedisced = .
spouse rafedisced, result(s5fedisced) wave(5)
label variable s5fedisced "s5fedisced:impflag w5 s education by isced code"
label values s5fedisced i_flag

****Education: ISCED code***ISCED file
**respondent education by ISCED code
*gen edisced`wv' = .
*replace edisced`wv'=.m if isced1997_r==. & inw`wv'==1
*replace edisced`wv'=.d if isced1997_r==-1
*replace edisced`wv'=.r if isced1997_r==-2
*replace edisced`wv'=.s if isced1997_r==95
*replace edisced`wv'=.o if isced1997_r==97
*replace edisced`wv' = isced1997_r if inrange(isced1997_r,0,6)

***Education, harmonized***
*respondent education harmonized (update)
replace raeducl = 1 if inrange(isced,0,2) & mi(raeducl)
replace raeducl = 2 if inrange(isced,3,4) & mi(raeducl)
replace raeducl = 3 if inrange(isced,5,6) & mi(raeducl)

*wave 5 spouse education harmonized
gen s5educl =.
spouse raeducl, result(s5educl) wave(5)
label variable s5educl "s5educl:w5 s harmonized education level"
label values s5educl edharm

*respondent education harmonized flag (update)
replace rafeducl = 0 if isced_f == 3 & mi(rafeducl)
replace rafeducl = 1 if isced_f == 14 & mi(rafeducl)

*wave 5 spouse education harmonized
gen s5feducl =.
spouse rafeducl, result(s5feducl) wave(5)
label variable s5feducl "s5feducl:impflag w5 s harmonized education level"
label values s5feducl i_flag

***current marital status: with partnerships***
*wave 5 respondent marital status with partnership
gen r5mstat= .
missing_common dn044_ dn014_, result(r5mstat)
replace r5mstat = r1mstat if r1mstat != . & dn044_ == 5
replace r5mstat = r2mstat if r2mstat != . & dn044_ == 5
replace r5mstat = r4mstat if r4mstat != . & dn044_ == 5
replace r5mstat = 1 if dn014_ == 1
replace r5mstat = 4 if dn014_ == 3
replace r5mstat = 5 if dn014_ == 5
replace r5mstat = 7 if dn014_ == 6
replace r5mstat = 8 if dn014_ == 4
replace r5mstat = 3 if (dn014_ == 2) | (inlist(r5mstat,4,5,7,8) & inlist(w5spouse,1,2))
label variable r5mstat "r5mstat:w5 r marital status w/partners, filled"

*wave 5 spouse marital status with partnership
gen s5mstat= .
spouse r5mstat, result(s5mstat) wave(5)
label variable s5mstat "s5mstat:w5 s marital status w/partners, filled"
label values r5mstat marwpart
label values s5mstat marwpart

***current marital status: current implied partnership status***
*wave 5 respondent implied partnership status
gen r5mpart =0 if inw5==1
replace r5mpart = 1 if inlist(dn014_, 3, 4, 5, 6) & inlist(w5spouse,1,2)
label variable r5mpart "r5mpart:w5 r implied partnership status"

*wave 5 spouse implied partnership status
gen s5mpart = .
spouse r5mpart, result(s5mpart) wave(5)
label variable s5mpart "s5mpart:w5 s implied partnership status"
label values r5mpart part
label values s5mpart part

***current marital status: without implied partnerships***
*wave 5 respondent marital status without implied partnership
gen r5mstath = .
missing_common dn014_, result(r5mstath)
replace r5mstath = r1mstath if r1mstath != . & dn044_ == 5
replace r5mstath = r2mstath if r2mstath != . & dn044_ == 5
replace r5mstath = r4mstath if r4mstath != . & dn044_ == 5
replace r5mstath = 1 if dn014_ == 1
replace r5mstath = 3 if dn014_ == 2
replace r5mstath = 4 if dn014_ == 3
replace r5mstath = 5 if dn014_ == 5
replace r5mstath = 7 if dn014_ == 6
replace r5mstath = 8 if dn014_ == 4
label variable r5mstath "r5mstath:w5 r marital status"

*wave 5 spouse marital status without implied partnership
gen s5mstath = .
spouse r5mstath, result(s5mstath) wave(5)
label variable s5mstath "s5mstath:w5 s marital status"
label values r5mstath  marwopart
label values s5mstath marwopart

***length of current marriage***
*wave 5 respondent length of current marriage
gen r5mcurln = .
missing_common dn015_ dn017_ int_year_w5, result(r5mcurln)
replace r5mcurln = int_year_w5 - dn015_ if inrange(dn015_,1900,2020) & inrange(int_year_w5,2000,2020)
replace r5mcurln = int_year_w5 - dn017_ if inrange(dn017_,1900,2020) & inrange(int_year_w5,2000,2020)
replace r5mcurln = .i if int_year_w5 < dn015_ & inrange(dn015_,1900,2020) & inrange(int_year_w5,2000,2020)
replace r5mcurln = .i if int_year_w5 < dn017_ & inrange(dn017_,1900,2020) & inrange(int_year_w5,2000,2020)
label variable r5mcurln "r5mcurln:w5 r length of current marriage"

*wave 5 spouse length of current marriage
gen s5mcurln = .
spouse r5mcurln, result(s5mcurln) wave(5)
label variable s5mcurln "s5mcurln:w5 s length of current marriage"

***place of birth***
*respondent place of birth (update)
missing_common dn004_ dn005c if missing(rabplace), result(rabplace)
replace rabplace = .n  if dn005c == -7 & missing(rabplace)
replace rabplace = 40  if country==11 & dn004_==1 & missing(rabplace)
replace rabplace = 276 if country==12 & dn004_==1 & missing(rabplace)
replace rabplace = 752 if country==13 & dn004_==1 & missing(rabplace)
replace rabplace = 528 if country==14 & dn004_==1 & missing(rabplace)
replace rabplace = 724 if country==15 & dn004_==1 & missing(rabplace)
replace rabplace = 380 if country==16 & dn004_==1 & missing(rabplace)
replace rabplace = 250 if country==17 & dn004_==1 & missing(rabplace)
replace rabplace = 208 if country==18 & dn004_==1 & missing(rabplace)
replace rabplace = 300 if country==19 & dn004_==1 & missing(rabplace)
replace rabplace = 756 if country==20 & dn004_==1 & missing(rabplace)
replace rabplace = 56  if country==23 & dn004_==1 & missing(rabplace)
replace rabplace = 376 if country==25 & dn004_==1 & missing(rabplace)
replace rabplace = 203 if country==28 & dn004_==1 & missing(rabplace)
replace rabplace = 616 if country==29 & dn004_==1 & missing(rabplace)
replace rabplace = 372 if country==30 & dn004_==1 & missing(rabplace)
replace rabplace = 442 if country==31 & dn004_==1 & missing(rabplace)
replace rabplace = 348 if country==32 & dn004_==1 & missing(rabplace)
replace rabplace = 620 if country==33 & dn004_==1 & missing(rabplace)
replace rabplace = 705 if country==34 & dn004_==1 & missing(rabplace)
replace rabplace = 233 if country==35 & dn004_==1 & missing(rabplace)
replace rabplace = dn005c if inrange(dn005c,1,1500)  & missing(rabplace)

*wave 5 spouse place of birth
gen s5bplace = .
spouse rabplace, result(s5bplace) wave(5)
label variable s5bplace "s5bplace:w`wv' s place of birth"
label values s5bplace countries

***Whether Born in Country***
*respondent whether born in country
missing_common dn004_ if mi(rabcountry), result(rabcountry)
replace rabcountry = .m if dn004_ == . & inw`wv'==1 & !inlist(rabcountry,0,1)
replace rabcountry = 0 if dn004_ == 5 & !inlist(rabcountry,0,1)
replace rabcountry = 1 if dn004_ == 1 & !inlist(rabcountry,0,1)
replace rabcountry = 0 if !inlist(rabcountry,0,1) & isocountry!=rabplace & !mi(isocountry) & !mi(rabplace)
replace rabcountry = 1 if !inlist(rabcountry,0,1) & isocountry==rabplace & !mi(isocountry) & !mi(rabplace)
label variable rabcountry "rabcountry: r born in country of interview"
label values rabcountry bornin

*wave 5 spouse whether born in country
gen s`wv'bcountry = .
spouse rabcountry, result(s`wv'bcountry) wave(`wv')
label variable s`wv'bcountry "s`wv'bcountry:w`wv' s born in country of interview"
label values s`wv'bcountry bornin

***rural or urban***
*household wave 5 rural or urban
gen h`wv'rural = .
missing_common iv009_, result(h`wv'rural)
replace h`wv'rural = .m if iv009_ == . & inw`wv' == 1
replace h`wv'rural = 0 if inrange(iv009_,1,4)
replace h`wv'rural = 1 if iv009_ == 5
label variable h`wv'rural "h`wv'rural:w`wv' lives in rural or urban"
label values h`wv'rural rural

forvalues p = 1 / 9 {
    bysort hhid5: replace h`wv'rural = h`wv'rural[_n-`p'] if hhid5 == hhid5[_n-`p'] & mi(h`wv'rural) & h`wv'rural[_n-`p'] !=. & inw`wv' == 1 // copy values to other household memembers
    bysort hhid5: replace h`wv'rural = h`wv'rural[_n+`p'] if hhid5 == hhid5[_n+`p'] & mi(h`wv'rural) & h`wv'rural[_n+`p'] !=. & inw`wv' == 1 // copy values to other household memembers
}

***Language of Interview***
*wave 5 respondent language of interview
gen r`wv'lang_s = .
replace r`wv'lang_s = 1 if inlist(language,11,12,20,42)
replace r`wv'lang_s = 2 if language == 13
replace r`wv'lang_s = 3 if language == 14
replace r`wv'lang_s = 4 if inlist(language,15,40)
replace r`wv'lang_s = 5 if inlist(language,16,22)
replace r`wv'lang_s = 6 if inlist(language,17,21,23,41)
replace r`wv'lang_s = 7 if language == 18
replace r`wv'lang_s = 8 if language == 19
replace r`wv'lang_s = 9 if language == 24
replace r`wv'lang_s = 10 if language == 25
replace r`wv'lang_s = 11 if language == 26
replace r`wv'lang_s = 12 if inlist(language,27,37)
replace r`wv'lang_s = 13 if language == 28
replace r`wv'lang_s = 14 if language == 29
replace r`wv'lang_s = 15 if language == 30
replace r`wv'lang_s = 16 if language == 32
replace r`wv'lang_s = 17 if inlist(language,33,43)
replace r`wv'lang_s = 18 if language == 34
replace r`wv'lang_s = 19 if language == 36
replace r`wv'lang_s = 20 if language == 39
replace r`wv'lang_s = 21 if language == 47
replace r`wv'lang_s = 22 if language == 31
replace r`wv'lang_s = 23 if language == 35
replace r`wv'lang_s = 24 if language == 38
replace r`wv'lang_s = 25 if language == 44
replace r`wv'lang_s = 26 if language == 45
label variable r`wv'lang_s "r`wv'lang_s:w`wv' r language of interview"
label values r`wv'lang_s languages

*wave 5 spouse language of interview
gen s`wv'lang_s = .
spouse r`wv'lang_s, result(s`wv'lang_s) wave(`wv')
label variable s`wv'lang_s "s`wv'lang_s:w`wv' s language of interview"
label values s`wv'lang_s languages


***drop cover screen tracker (respondent) file raw variables
drop `demo_all_cv_r'

***drop wave 5 Asset file raw variables
drop `demo_w5_as'

***drop wave 5 Behavioural Risk file raw variables
drop `demo_w5_br'

***drop wave 5 Children file raw variables
drop `demo_w5_ch'

***drop wave 5 Consumption file raw variables
drop `demo_w5_co'

***drop wave 5 Demographic file raw variables
drop `demo_w5_dn'

***drop wave 5 Employment and Pension file raw variables
drop `demo_w5_ep'

***drop wave 5 Financial Transfer file raw variables
drop `demo_w5_ft'

***drop wave 5 Household Income file raw variables
drop `demo_w5_hh'

***drop wave 5 Housing file raw variables
drop `demo_w5_ho'

***drop wave 5 interviewer file raw variables
drop `demo_w5_iv'

***drop wave 5 Physical Health file raw variables
drop `demo_w5_ph'

***drop wave 5 Social Support file raw variables
drop `demo_w5_sp'

***drop wave 5 Dropoff file raw variables
drop `demo_w5_dropoff'

***drop wave 5 Generated Variables (Weights) file raw variables
drop `demo_w5_gv_weights'

***drop Longititudinal Weights wave 4 wave 5 file raw variables
drop `demo_long_weight_w4w5'

***drop Longititudinal Weights wave 1 - wave 5 file raw variables
*drop `demo_long_weight_w1w5'

***drop wave 5 Generated Variables (Imputations) file raw variables
drop `demo_w5_imputations'

***drop wave 1 Generated Variables (ISCED) file raw variables
drop `demo_w5_isced'


******************************************************************************************


*set wave number
local wv = 6

***merge with cover screen tracker (respondent) data***
local demo_all_cv_r mergeidp6 int_year_w6 int_month_w6 interview_hh_w6 ///
                fam_resp_w6 fin_resp_w6 hou_resp_w6 partnerinhh_w6 coupleid5 ///
                gender yrbirth mobirth country waveid_hh firstwave_hh ///
                deadoralive_w5 deadoralive_w6 deceased_year deceased_month deceased_age agep2015
merge 1:1 mergeid using "$tracker", keepusing(`demo_all_cv_r' hhid6 age_int_w6 interview_w6) nogen

***merge with wave 6 Asset data***
local demo_w6_as as057_
merge 1:1 mergeid using "$wave_6_as", keepusing(`demo_w6_as') nogen

***merge with wave 6 Behavioural Risk data***
local demo_w6_br br017_
merge 1:1 mergeid using "$wave_6_br", keepusing(`demo_w6_br') nogen

***merge with wave 6 Children data***
local demo_w6_ch ch023_
merge 1:1 mergeid using "$wave_6_ch", keepusing(`demo_w6_ch') nogen

***merge with wave 6 Consumption data***
local demo_w6_co co009_
merge 1:1 mergeid using "$wave_6_co", keepusing(`demo_w6_co') nogen

***merge with wave 6 Demographic data***
local demo_w6_dn dn041_ dn014_ dn044_ dn015_ dn017_ dn004_ dn005c language dn002_ dn042_
merge 1:1 mergeid using "$wave_6_dn", keepusing(`demo_w6_dn') nogen

***merge with wave 6 Employment and Pension data***
local demo_w6_ep ep210_
merge 1:1 mergeid using "$wave_6_ep", keepusing(`demo_w6_ep') nogen

***merge with wave 6 Financial Transfer data***
local demo_w6_ft ft021_
merge 1:1 mergeid using "$wave_6_ft", keepusing(`demo_w6_ft') nogen

***merge with wave 6 Household Income data***
local demo_w6_hh hh014_
merge 1:1 mergeid using "$wave_6_hh", keepusing(`demo_w6_hh') nogen

***merge with wave 6 Housing data***
local demo_w6_ho ho041_
merge 1:1 mergeid using "$wave_6_ho", keepusing(`demo_w6_ho') nogen

***merge with wave 5 interviewer data***
local demo_w6_iv iv009_
merge 1:1 mergeid using "$wave_6_iv", keepusing(`demo_w6_iv') nogen

***merge with wave 6 Physical Health data***
local demo_w6_ph ph054_
merge 1:1 mergeid using "$wave_6_ph", keepusing(`demo_w6_ph') nogen

***merge with wave 6 Social Support data***
local demo_w6_sp sp022_
merge 1:1 mergeid using "$wave_6_sp", keepusing(`demo_w6_sp') nogen

***merge with wave 6 Dropoff data***
local demo_w6_dropoff il_q1_a pl_q1 si_q1a
merge 1:1 mergeid using "$wave_6_dropoff", keepusing(`demo_w6_dropoff') nogen

***merge with wave 6 Generated Variables (Weights) data***
local demo_w6_gv_weights cchw_w6 cciw_w6 dw_w6 ///
                         psu ssu stratum1 stratum2
merge 1:1 mergeid using "$wave_6_gv_weights", keepusing(`demo_w6_gv_weights') nogen

***merge with wave 6 Generated Variables (Imputations) data***
local demo_w6_imputations isced isced_f yedu yedu_f implicat p_nrp
merge 1:1 mergeid using "$wave_6_imputations_", keepusing(`demo_w6_imputations') nogen

***merge with Longititudinal Weights wave 5 wave 6 data***
local demo_long_weight_w5w6 clhw_f cliw_f
merge 1:1 mergeid using "$long_weight_w5w6", keepusing(`demo_long_weight_w5w6') nogen

***merge with Longititudinal Weights wave 1 - wave 6 data***
local demo_long_weight_w1w6 clhw_a cliw_a
merge 1:1 mergeid using "$long_weight_w1w6", keepusing(`demo_long_weight_w1w6') nogen

***merge with wave 1 Generated Variables ISCED data***
local demo_w6_isced isced1997_r 
merge 1:1 mergeid using "$wave_6_gv_isced", keepusing(`demo_w6_isced') nogen


***wave status: response indicator***
*wave 1 response indicator (update)
replace inw1 = 0 if missing(inw1)

*wave 2 response indicator (update)
replace inw2 = 0 if missing(inw2)

*wave 3 response indicator (update)
replace inw3 = 0 if missing(inw4)

*wave 4 response indicator (update)
replace inw4 = 0 if missing(inw4)

*wave 5 response indicator (update)
replace inw5 = 0 if missing(inw5)

*wave 6 response indicator
gen inw6 = 0
replace inw6 = 1 if interview_w6 == 1
label variable inw6 "inw6: =1 if respondent w6"
label values inw6 inw

***wave status: drop-off response indicator***
*wave 6 response indicator
gen inw6sc = 0
replace inw6sc = 1 if !mi(il_q1_a) | !mi(pl_q1) | !mi(si_q1a)
label variable inw6sc "inw6sc: =1 if respondent w6 drop-off"
label values inw6sc inw

***person specific identifier***
*country id (char)
replace cid = substr(mergeid,1,2) if missing(cid)
replace cid = "11" if cid == "AT"
replace cid = "12" if cid == "DE"
replace cid = "13" if cid == "SE"
replace cid = "14" if cid == "NL"
replace cid = "15" if cid == "ES" | cid == "Eg"
replace cid = "16" if cid == "IT"
replace cid = "17" if cid == "FR" | cid == "F1"
replace cid = "18" if cid == "DK"
replace cid = "19" if cid == "GR"
replace cid = "20" if cid == "Cg"
replace cid = "21" if cid == "Cf"
replace cid = "22" if cid == "Ci"
replace cid = "23" if cid == "Bf"
replace cid = "24" if cid == "Bn"
replace cid = "25" if cid == "Ih"
replace cid = "26" if cid == "Ia"
replace cid = "27" if cid == "Ir"
replace cid = "28" if cid == "CZ"
replace cid = "29" if cid == "PL"
replace cid = "30" if cid == "IE"
replace cid = "31" if cid == "LU"
replace cid = "32" if cid == "HU"
replace cid = "33" if cid == "PT"
replace cid = "34" if cid == "SI"
replace cid = "35" if cid == "EE"
replace cid = "47" if cid == "HR"

*household id (char) (update)
replace hhidc = substr(mergeid,4,6) if missing(hhidc)

*person number (char) (update)
replace pn = substr(mergeid,11,2) if missing(pn)
*note: pn = a5, a6, b5, b6 are coverscreen respondents outside of the household
*we convert them to numbers to create a numerical version of nmergeid
replace pn = "90" if pn == "a5"
replace pn = "91" if pn == "a6"
replace pn = "92" if pn == "b5"
replace pn = "93" if pn == "b6"

*merge identifier (num) (update)
tempvar w6mergeid
gen `w6mergeid' =cid+hhidc+pn
destring `w6mergeid', replace
replace nmergeid = `w6mergeid' if missing(nmergeid)

***household identifier***
*wave 6 household id (char)
tempvar subhh hhidc
gen `subhh' = substr(hhid6,11,1)
replace `subhh' = "1" if `subhh'=="A"
replace `subhh' = "2" if `subhh'=="B"
gen `hhidc' = substr(hhid6,4,6)
gen hh6hhidc =""
replace hh6hhidc = cid + `hhidc' + `subhh' if inw6 == 1
label variable hh6hhidc "hh6hhidc:w6 cid + hhold id + subhousehold /9-char"

*wave 6 household id (num)
destring hh6hhidc, generate(hh6hhid)
label variable hh6hhid "hh6hhid:w6 cid + hhold id + subhousehold /Num"
format hh6hhid %9.0f
label variable hhid6 "hhid6: SHARE household identifier wave 6"

***Country*** (update)
replace racountry = country if mi(racountry)
replace isocountry = 040 if country == 11
replace isocountry = 276 if country == 12
replace isocountry = 752 if country == 13
replace isocountry = 528 if country == 14
replace isocountry = 724 if country == 15
replace isocountry = 380 if country == 16
replace isocountry = 250 if country == 17
replace isocountry = 208 if country == 18
replace isocountry = 300 if country == 19
replace isocountry = 756 if country == 20
replace isocountry = 056 if country == 23
replace isocountry = 376 if country == 25
replace isocountry = 203 if country == 28
replace isocountry = 616 if country == 29
replace isocountry = 372 if country == 30
replace isocountry = 442 if country == 31
replace isocountry = 348 if country == 32
replace isocountry = 620 if country == 33
replace isocountry = 705 if inlist(country, 34, 43)
replace isocountry = 233 if country == 35
replace isocountry = 191 if country == 47

***Implicate***
replace implicate = implicat if mi(implicate)

***couple identifier***
*wave 6 couple id (number)
gen pnp = substr(mergeidp6,11,2)

gen hh_coup=""
levelsof pn if !mi(hhid6), local(pns)
foreach r of local pns {
	forvalues i = 1/9 {
		bysort hhid6: replace hh_coup = "`r'"+"`i'" ///
			if pn == pnp[_n-`i'] & pn == "`r'"
		bysort hhid6: replace hh_coup ="`r'"+"`i'" ///
			if hh_coup[_n+`i'] == "`r'"+"`i'"
	}
}
replace hh_coup=string(_n) if missing(hh_coup)
egen h6coupid = group(hhid6 hh_coup)
label variable h6coupid "wave 6 specific couple id number /num"
drop hh_coup pnp

***Spouse Identifier***
*wave 6 spouse id (char)
gen s6mergeid = "0" if inw6 == 1
bysort h6coupid: replace s6mergeid = mergeid[_n+1] if !missing(mergeid[_n+1]) & !missing(h6coupid)
bysort h6coupid: replace s6mergeid = mergeid[_n-1] if !missing(mergeid[_n-1]) & !missing(h6coupid)
label variable s6mergeid "s6mergeid:w6 spouse mergeid /12-char"

*wave 6 spouse id (num)
gen s6nmergeid =""
replace s6nmergeid = "0"  if s6mergeid=="0"

tempvar s6_cid s6_hhidc s6_pn

gen `s6_cid' = substr(s6mergeid,1,2)
replace `s6_cid' = "11" if `s6_cid' == "AT"
replace `s6_cid' = "12" if `s6_cid' == "DE"
replace `s6_cid' = "13" if `s6_cid' == "SE"
replace `s6_cid' = "14" if `s6_cid' == "NL"
replace `s6_cid' = "15" if `s6_cid' == "ES" | `s6_cid' == "Eg"
replace `s6_cid' = "16" if `s6_cid' == "IT"
replace `s6_cid' = "17" if `s6_cid' == "FR" | `s6_cid' == "F1"
replace `s6_cid' = "18" if `s6_cid' == "DK"
replace `s6_cid' = "19" if `s6_cid' == "GR"
replace `s6_cid' = "20" if `s6_cid' == "Cg"
replace `s6_cid' = "21" if `s6_cid' == "Cf"
replace `s6_cid' = "22" if `s6_cid' == "Ci"
replace `s6_cid' = "23" if `s6_cid' == "Bf"
replace `s6_cid' = "24" if `s6_cid' == "Bn"
replace `s6_cid' = "25" if `s6_cid' == "Ih"
replace `s6_cid' = "26" if `s6_cid' == "Ia"
replace `s6_cid' = "27" if `s6_cid' == "Ir"
replace `s6_cid' = "28" if `s6_cid' == "CZ"
replace `s6_cid' = "29" if `s6_cid' == "PL"
replace `s6_cid' = "30" if `s6_cid' == "IE"
replace `s6_cid' = "31" if `s6_cid' == "LU"
replace `s6_cid' = "32" if `s6_cid' == "HU"
replace `s6_cid' = "33" if `s6_cid' == "PT"
replace `s6_cid' = "34" if `s6_cid' == "SI"
replace `s6_cid' = "35" if `s6_cid' == "EE"
replace `s6_cid' = "47" if `s6_cid' == "HR"

gen `s6_hhidc' = substr(s6mergeid,4,6)

gen `s6_pn' = substr(s6mergeid,11,2)

replace s6nmergeid =`s6_cid'+`s6_hhidc'+`s6_pn' if missing(s6nmergeid)
label variable s6nmergeid "s6nmergeid:w6 spouse mergeid /num"

*respondent number of spouse id's (update)
replace raspct = raspct + 1 if s6nmergeid != s1nmergeid & s6nmergeid != s2nmergeid & s6nmergeid != s4nmergeid & s6nmergeid != s5nmergeid & !inlist(s6nmergeid,"0","")
replace raspct = 0 if s6nmergeid == "0" & missing(raspct)
replace raspct = 1 if !inlist(s6nmergeid,"0","") & missing(raspct)

*respondent first spouse id (update)
replace raspid1 = s6nmergeid if !inlist(s6nmergeid,"0","") & missing(raspid1)
codebook raspid1

*respondent second spouse id (update)
replace raspid2 = s6nmergeid if s6nmergeid != raspid1 & !inlist(raspid1,"0","") & ///
                                !inlist(s6nmergeid,"0","") & mi(raspid2)
codebook raspid2

*respondent third spouse id
replace raspid3 = s6nmergeid if s6nmergeid != raspid1 & !inlist(raspid1,"0","") & ///
                                s6nmergeid != raspid2 & !inlist(raspid2,"0","") & /// 
                                !inlist(s6nmergeid,"0","") & mi(raspid3)
codebook raspid3

*respondent fourth spouse id
replace raspid4 = s6nmergeid if s6nmergeid != raspid1 & !inlist(raspid1,"0","") & ///
								s6nmergeid != raspid2 & !inlist(raspid2,"0","") & ///
								s6nmergeid != raspid3 & !inlist(raspid3,"0","") & ///
								!inlist(s6nmergeid,"0","") & mi(raspid4)
codebook raspid4

*create temporary indicator of wave 6 spouse
gen w6spouse=0 if missing(mergeidp6) & inw6==1
bysort h6coupid: replace w6spouse = 1 if !mi(mergeidp6) & inw6[_n-1] == 1 & inw6 == 1 & !mi(h6coupid)
bysort h6coupid: replace w6spouse = 1 if !mi(mergeidp6) & inw6[_n+1] == 1 & inw6 == 1 & !mi(h6coupid)
bysort h6coupid: replace w6spouse = 2 if !mi(mergeidp6) & inw6[_n-1] == 0 & inw6 == 1 & !mi(h6coupid)
bysort h6coupid: replace w6spouse = 2 if !mi(mergeidp6) & inw6[_n+1] == 0 & inw6 == 1 & !mi(h6coupid)

***wave status: interview status***
*wave 1 interview status (update)
replace r1iwstat = 0 if missing(r1iwstat)

*wave 2 interview status (update)
replace r2iwstat = 0 if missing(r2iwstat)

*wave 3 interview status (update)
replace r3iwstat = 0 if missing(r3iwstat)

*wave 4 interview status (update)
replace r4iwstat = 0 if missing(r4iwstat)

*wave 5 interview status (update)
replace r5iwstat = 0 if missing(r5iwstat)

*wave 6 interview status
gen r6iwstat = .
replace r6iwstat = 0
replace r6iwstat = 1 if inw6 == 1
replace r6iwstat = 4 if inw6 == 0 & deadoralive_w6 == 1
replace r6iwstat = 5 if inw6 == 0 & deadoralive_w5 == 1 & deadoralive_w6 == 2
replace r6iwstat = 6 if inw6 == 0 & deadoralive_w5 == 2 & deadoralive_w6 == 2
replace r6iwstat = 9 if inw6 == 0 & deadoralive_w6 == 0
label variable r6iwstat "r6iwstat:w6 r interview status"
label values r6iwstat wstat

*wave 6 spouse interview status
gen s6iwstat = .
spouse r6iwstat, result(s6iwstat) wave(6)
label variable s6iwstat "s6iwstat:w6 s interview status"
label values s6iwstat wstat

***cohort***
*sample cohort (update)
replace hacohort = 1 if waveid_hh == 142 & country == 47 & mi(hacohort)
replace hacohort = 5 if waveid_hh == 142 & inlist(country,16,17,18,19,23,29,31,34,35) & mi(hacohort)

****Wave in which the household was sampled***
*household wave in which sampled (update)
replace hawvsamp = firstwave_hh if missing(hawvsamp)

***sampling weight***
*sampling weight, combined sample
gen r6wtsamp =.
replace r6wtsamp = dw_w6
label variable r6wtsamp "r6wtsamp:w6 design weight"

*respondent psu (update)
replace rapsu1 = real(psu) if mi(rapsu1)

*respondent psu 2 (update)
replace rapsu2 = real(ssu) if mi(rapsu2)

*respondent stratification variable (update)
replace raestrat1 = stratum1 if mi(stratum1)

*respondent stratification variable 2 (update)
replace raestrat2 = stratum2 if mi(raestrat2)

***Household analysis weight***
*wave 6 household analysis weight, combined sample
gen r6wthh =.
replace r6wthh = cchw_w6
label variable r6wthh "r6wthh:w6 household analysis weight"

***person-level analysis weight***
*wave 6 respondent analysis weight, combined sample
gen r6wtresp =.
replace r6wtresp = cciw_w6
label variable r6wtresp "r6wtresp:w6 person-level analysis weight"

***household longitudinal weight***
*household longitudinal weight, combined sample
gen ral56wthh =.
replace ral56wthh = clhw_f
label variable ral56wthh "ral56wthh:w6 household longitudinal w5-w6 weight"

***person-level longitudinal weight***
*respondent longitudinal weight, combined sample
gen ral56wtrsp =.
replace ral56wtrsp = cliw_f
label variable ral56wtrsp "ral56wtrsp:w6 person-level longitudinal w5-w6 weight"

***household longitudinal weight***
*household longitudinal weight, combined sample
gen ral16wthh =.
replace ral16wthh = clhw_a
label variable ral16wthh "ral16wthh:w6 household longitudinal w1-w6 weight"

***person-level longitudinal weight***
*respondent longitudinal weight, combined sample
gen ral16wtrsp =.
replace ral16wtrsp = cliw_a
label variable ral16wtrsp "ral16wtrsp:w6 person-level longitudinal w1-w6 weight"

***number of household respondents***
*wave 6 household number of respondents 
egen hh6hhresp = count(inw6==1) if inw6==1, by(hh6hhid)
label variable hh6hhresp "hh6hhresp:w6 # core respondents in hh"

***whether coupled***
*wave 6 couple indicator
egen h6cpl = sum(inw6==1) if inw6==1, by(h6coupid)
recode h6cpl (0= .) (1=0) (2=1)
label variable h6cpl "h6cpl:w6 whether coupled"
label values h6cpl cpl

***financial respondent***
*wave 6 respondent financial respondent indicator
gen r6finr =.
replace r6finr = 0 if fin_resp_w6 == 0
replace r6finr = 1 if fin_resp_w6 == 1
label variable r6finr "r6finr:w6 whether r financial resp"
label values r6finr finr

*wave 6 spouse financial respondent indicator
gen s6finr =.
spouse r6finr, result(s6finr) wave(6)
label variable s6finr "s6finr:w6 whether s financial resp"
label values s6finr  finr

***hosuehold respondent***
*wave 6 respondent household respondent indicator
gen r6hhr =.
replace r6hhr = 1 if hou_resp_w6 == 1
replace r6hhr = 0 if hou_resp_w6 == 0
label variable r6hhr "r6hhr:w6 whether r hh resp"
label values r6hhr hhr

*wave 6 spouse household respondent indicator
gen s6hhr =.
spouse r6hhr, result(s6hhr) wave(6)
label variable s6hhr "s6hhr:w6 whether s hh resp"
label values s6hhr hhr

***family respondent***
*wave 6 respondent family respondent indicator
gen r6famr =.
replace r6famr = 1 if fam_resp_w6 == 1
replace r6famr = 0 if fam_resp_w6 == 0
label variable r6famr "r6famr:w6 whether r family resp"
label values r6famr famr

*wave 1 spouse family respondent indicator
gen s6famr =.
spouse r6famr, result(s6famr) wave(6)
label variable s6famr "s6famr:w6 whether s family resp"
label values s6famr  famr

***whether proxy interview***
*wave 6 respondent proxy indicator
gen r6proxy = .
missing_common ph054 br017 ep210 ch023 sp022 ft021 ho041 hh014 co009 as057, result(r6proxy)
replace r6proxy = 0 if ph054 ==1 ///
					 & br017 ==1 ///
					 & ep210 ==1 ///
					 & ch023 ==1 ///
					 & sp022 ==1 ///
					 & ft021 ==1 ///
					 & ho041 ==1 ///
					 & hh014 ==1 ///
					 & co009 ==1 ///
					 & as057 ==1
replace r6proxy = 1 if ( inlist(ph054,1,2,3) ///
    					 | inlist(br017,1,2,3) ///
    					 | inlist(ep210,1,2,3) ///
    					 | inlist(ch023,1,2,3) ///
    					 | inlist(sp022,1,2,3) ///
    					 | inlist(ft021,1,2,3) ///
    					 | inlist(ho041,1,2,3) ///
    					 | inlist(hh014,1,2,3) ///
    					 | inlist(co009,1,2,3) ///
    					 | inlist(as057,1,2,3) ) & missing(r6proxy)
replace r6proxy = 2 if ph054 ==3 ///
					 & br017 ==3 ///
					 & ep210 ==3 ///
					 & ch023 ==3 ///
					 & sp022 ==3 ///
					 & ft021 ==3 ///
					 & ho041 ==3 ///
					 & hh014 ==3 ///
					 & co009 ==3 ///
					 & as057 ==3
label variable r6proxy "r6proxy:w6 whether r proxy interview"

*wave 6 spouse proxy indicator
gen s6proxy = .
spouse r6proxy, result(s6proxy) wave(6)
label variable s6proxy "s6proxy:w6 whether s proxy interview"
label values r6proxy proxy
label values s6proxy proxy

***interview date***
*wave 6 respondent household interview month
gen r6iwm = .
missing_common int_month_w6 if interview_w6 == 1, result(r6iwm)
replace r6iwm = int_month_w6 if inrange(int_month_w6,1,12) & interview_w6 == 1
label variable r6iwm "r6iwm:w6 household interview month"

*wave 6 respondent household interview year
gen r6iwy = .
missing_common int_year_w6 if interview_w6 == 1, result(r6iwy)
replace r6iwy = int_year_w6 if inrange(int_year_w6,2000, 2020) & interview_w6 == 1
label variable r6iwy "r6iwy:w6 household interview year"

*wave 6 spouse household interview month
gen s6iwm = .
spouse r6iwm, result(s6iwm) wave(6)
label variable s6iwm "s6iwm:w6 household interview month"

*wave 6 spouse household interview year
gen s6iwy = .
spouse r6iwy, result(s6iwy) wave(6)
label variable s6iwy "s6iwy:w6 household interview year"

***birth date***
*respondent birth year (update)
missing_common yrbirth  if missing(rabyear), result(rabyear)
replace rabyear = yrbirth if inrange(yrbirth,1890,2020) & missing(rabyear)

*wave 6 spouse birth year
gen s6byear = .
spouse rabyear, result(s6byear) wave(6)
label variable s6byear "s6byear: s birth year"

*wave 6 respondent birth month
gen r6bmonth =.
missing_common dn002_, result(r6bmonth)
replace r6bmonth = dn002_ if inrange(dn002_,1,12)

***death date***
*respondent death year (update)
missing_common deceased_year if mi(radyear), result(radyear)
replace radyear = .i if deceased_year == -3 & mi(radyear)
replace radyear = deceased_year if inrange(deceased_year,2000,2020) & mi(radyear)

*respondent death month (update)
missing_common deceased_month if mi(radmonth), result(radmonth)
replace radmonth = .i if deceased_month == -3 & mi(radmonth)
replace radmonth = deceased_month if inrange(deceased_month,1,12) & mi(radmonth)

***SHARE sampling eligibility***
*wave 6 spouse sampling eligibility
gen s6sampelig= .
spouse rasampelig, result(s6sampelig) wave(6)
label variable s6sampelig "s6sampelig:w6 s eligibility at w1 sampling"
label values s6sampelig share_status

****gender***
*respondent gender (update)
*missing_common gender if missing(ragender), result(ragender)
*replace ragender = 1 if gender == 1 & missing(ragender)
*replace ragender = 2 if gender == 2 & missing(ragender)
*
**wave 5 spouse gender
*gen s5gender = .
*spouse ragender, result(s5gender) wave(5)
*label variable s5gender "s5gender:w5 s gender"
*label values s5gender gender

***gender***
*respondent gender (update)
missing_common dn042_ if missing(ragender), result(ragender)
replace ragender = 1 if dn042_ == 1 & missing(ragender)
replace ragender = 2 if dn042_ == 2 & missing(ragender)

*wave 6 spouse gender
gen s6gender = .
spouse ragender, result(s6gender) wave(6)
replace s6gender = .v if s6iwstat==4
label variable s6gender "s6gender:w6 s gender"
label values s6gender gender

***education: years of education***
*respondent education in years (update)
replace raedyrs = round(yedu) if inrange(yedu,0,21) & mi(raedyrs)

*wave 6 spouse education in years
gen s6edyrs =.
spouse raedyrs, result(s6edyrs) wave(6)
label variable s6edyrs "s6edyrs:w6 s years of education"

*respondent years of education flag (update)
replace rafedyrs = 0 if yedu_f == 3 & mi(rafedyrs)
replace rafedyrs = 1 if yedu_f == 14 & mi(rafedyrs)

*wave 6 spouse education in years flag
gen s6fedyrs =.
spouse rafedyrs, result(s6fedyrs) wave(6)
label variable s6fedyrs "s6fedyrs:impflag w6 s years of education"
label values s6fedyrs i_flag

***Education: ISCED code***
*respondent education by ISCED code (update)
replace raedisced = isced if inrange(isced,0,6) & mi(raedisced)

*wave 6 spouse education by ISCED code
gen s6edisced = .
spouse raedisced, result(s6edisced) wave(6)
label variable s6edisced "s6edisced:w6 s education by isced code"
label values s6edisced edisced

*respondent education by ISCED code flag (update)
replace rafedisced = 0 if isced_f == 3 & mi(rafedisced)
replace rafedisced = 1 if isced_f == 14 & mi(rafedisced)

*wave 6 spouse education by ISCED code
gen s6fedisced = .
spouse rafedisced, result(s6fedisced) wave(6)
label variable s6fedisced "s6fedisced:impflag w6 s education by isced code"
label values s6fedisced i_flag

****Education: ISCED code***ISCED file
**respondent education by ISCED code
*gen edisced6 = .
*replace edisced6=.m if isced1997_r==. & inw6==1
*replace edisced6=.d if isced1997_r==-1
*replace edisced6=.r if isced1997_r==-2
*replace edisced6=.s if isced1997_r==95
*replace edisced6=.o if isced1997_r==97
*replace edisced6 = isced1997_r if inrange(isced1997_r,0,6)

***Education, harmonized***
*respondent education harmonized (update)
replace raeducl = 1 if inrange(isced,0,2) & mi(raeducl)
replace raeducl = 2 if inrange(isced,3,4) & mi(raeducl)
replace raeducl = 3 if inrange(isced,5,6) & mi(raeducl)

*wave 6 spouse education harmonized
gen s6educl =.
spouse raeducl, result(s6educl) wave(6)
label variable s6educl "s6educl:w6 s harmonized education level"
label values s6educl edharm

*respondent education harmonized flag (update)
replace rafeducl = 0 if isced_f == 3 & mi(rafeducl)
replace rafeducl = 1 if isced_f == 14 & mi(rafeducl)

*wave 6 spouse education harmonized
gen s6feducl =.
spouse rafeducl, result(s6feducl) wave(6)
label variable s6feducl "s6feducl:impflag w6 s harmonized education level"
label values s6feducl i_flag

***current marital status: with partnerships***
*wave 6 respondent marital status with partnership
gen r6mstat= .
missing_common dn044_ dn014_, result(r6mstat)
replace r6mstat = r1mstat if r1mstat != . & dn044_ == 5
replace r6mstat = r2mstat if r2mstat != . & dn044_ == 5
replace r6mstat = r4mstat if r4mstat != . & dn044_ == 5
replace r6mstat = r5mstat if r5mstat != . & dn044_ == 5
replace r6mstat = 1 if dn014_ == 1
replace r6mstat = 4 if dn014_ == 3
replace r6mstat = 5 if dn014_ == 5
replace r6mstat = 7 if dn014_ == 6
replace r6mstat = 8 if dn014_ == 4
replace r6mstat = 3 if (dn014_ == 2) | (inlist(r6mstat,4,5,7,8) & inlist(w6spouse,1,2))
label variable r6mstat "r6mstat:w6 r marital status w/partners, filled"

*wave 6 spouse marital status with partnership
gen s6mstat= .
spouse r6mstat, result(s6mstat) wave(6)
label variable s6mstat "s6mstat:w6 s marital status w/partners, filled"
label values r6mstat marwpart
label values s6mstat marwpart

***current marital status: current implied partnership status***
*wave 6 respondent implied partnership status
gen r6mpart =0 if inw6==1
replace r6mpart = 1 if inlist(dn014_, 3, 4, 5, 6) & inlist(w6spouse,1,2)
label variable r6mpart "r6mpart:w6 r implied partnership status"

*wave 6 spouse implied partnership status
gen s6mpart = .
spouse r6mpart, result(s6mpart) wave(6)
label variable s6mpart "s6mpart:w6 s implied partnership status"
label values r6mpart part
label values s6mpart part

***current marital status: without implied partnerships***
*wave 6 respondent marital status without implied partnership
gen r6mstath = .
missing_common dn014_, result(r6mstath)
replace r6mstath = r1mstath if r1mstath != . & dn044_ == 5
replace r6mstath = r2mstath if r2mstath != . & dn044_ == 5
replace r6mstath = r4mstath if r4mstath != . & dn044_ == 5
replace r6mstath = r5mstath if r5mstath != . & dn044_ == 5
replace r6mstath = 1 if dn014_ == 1
replace r6mstath = 3 if dn014_ == 2
replace r6mstath = 4 if dn014_ == 3
replace r6mstath = 5 if dn014_ == 5
replace r6mstath = 7 if dn014_ == 6
replace r6mstath = 8 if dn014_ == 4
label variable r6mstath "r6mstath:w6 r marital status"

*wave 6 spouse marital status without implied partnership
gen s6mstath = .
spouse r6mstath, result(s6mstath) wave(6)
label variable s6mstath "s6mstath:w6 s marital status"
label values r6mstath  marwopart
label values s6mstath marwopart

***length of current marriage***
*wave 6 respondent length of current marriage
gen r6mcurln = .
missing_common dn015_ dn017_ int_year_w6, result(r6mcurln)
replace r6mcurln = int_year_w6 - dn015_ if inrange(dn015_,1900,2020) & inrange(int_year_w6,2000,2020)
replace r6mcurln = int_year_w6 - dn017_ if inrange(dn017_,1900,2020) & inrange(int_year_w6,2000,2020)
replace r6mcurln = .i if int_year_w6 < dn015_ & inrange(dn015_,1900,2020) & inrange(int_year_w6,2000,2020)
replace r6mcurln = .i if int_year_w6 < dn017_ & inrange(dn017_,1900,2020) & inrange(int_year_w6,2000,2020)
label variable r6mcurln "r6mcurln:w6 r length of current marriage"

*wave 6 spouse length of current marriage
gen s6mcurln = .
spouse r6mcurln, result(s6mcurln) wave(6)
label variable s6mcurln "s6mcurln:w6 s length of current marriage"

***place of birth***
*respondent place of birth (update)
missing_common dn004_ dn005c if missing(rabplace), result(rabplace)
replace rabplace = .n  if dn005c == -7 & missing(rabplace)
replace rabplace = 40  if country==11 & dn004_==1 & missing(rabplace)
replace rabplace = 276 if country==12 & dn004_==1 & missing(rabplace)
replace rabplace = 752 if country==13 & dn004_==1 & missing(rabplace)
replace rabplace = 528 if country==14 & dn004_==1 & missing(rabplace)
replace rabplace = 724 if country==15 & dn004_==1 & missing(rabplace)
replace rabplace = 380 if country==16 & dn004_==1 & missing(rabplace)
replace rabplace = 250 if country==17 & dn004_==1 & missing(rabplace)
replace rabplace = 208 if country==18 & dn004_==1 & missing(rabplace)
replace rabplace = 300 if country==19 & dn004_==1 & missing(rabplace)
replace rabplace = 756 if country==20 & dn004_==1 & missing(rabplace)
replace rabplace = 56  if country==23 & dn004_==1 & missing(rabplace)
replace rabplace = 376 if country==25 & dn004_==1 & missing(rabplace)
replace rabplace = 203 if country==28 & dn004_==1 & missing(rabplace)
replace rabplace = 616 if country==29 & dn004_==1 & missing(rabplace)
replace rabplace = 372 if country==30 & dn004_==1 & missing(rabplace)
replace rabplace = 442 if country==31 & dn004_==1 & missing(rabplace)
replace rabplace = 348 if country==32 & dn004_==1 & missing(rabplace)
replace rabplace = 620 if country==33 & dn004_==1 & missing(rabplace)
replace rabplace = 705 if country==34 & dn004_==1 & missing(rabplace)
replace rabplace = 233 if country==35 & dn004_==1 & missing(rabplace)
replace rabplace = 191 if country==47 & dn004_==1 & missing(rabplace)
replace rabplace = dn005c if inrange(dn005c,1,1500)  & missing(rabplace)

*wave 6 spouse place of birth
gen s6bplace = .
spouse rabplace, result(s6bplace) wave(6)
label variable s6bplace "s6bplace:w6 s place of birth"
label values s6bplace countries

***Whether Born in Country***
*respondent whether born in country
missing_common dn004_ if mi(rabcountry), result(rabcountry)
replace rabcountry = .m if dn004_ == . & inw6==1 & !inlist(rabcountry,0,1)
replace rabcountry = 0 if dn004_ == 5 & !inlist(rabcountry,0,1)
replace rabcountry = 1 if dn004_ == 1 & !inlist(rabcountry,0,1)
replace rabcountry = 0 if !inlist(rabcountry,0,1) & isocountry!=rabplace & !mi(isocountry) & !mi(rabplace)
replace rabcountry = 1 if !inlist(rabcountry,0,1) & isocountry==rabplace & !mi(isocountry) & !mi(rabplace)
label variable rabcountry "rabcountry: r born in country of interview"
label values rabcountry bornin

*wave 6 spouse whether born in country
gen s6bcountry = .
spouse rabcountry, result(s6bcountry) wave(6)
label variable s6bcountry "s6bcountry:w6 s born in country of interview"
label values s6bcountry bornin

***rural or urban***
*household wave 6 rural or urban
gen h6rural = .
missing_common iv009_, result(h6rural)
replace h6rural = .m if iv009_ == . & inw6 == 1
replace h6rural = 0 if inrange(iv009_,1,4)
replace h6rural = 1 if iv009_ == 5
label variable h6rural "h6rural:w6 lives in rural or urban"
label values h6rural rural

forvalues p = 1 / 9 {
    bysort hhid6: replace h6rural = h6rural[_n-`p'] if hhid6 == hhid6[_n-`p'] & mi(h6rural) & h6rural[_n-`p'] !=. & inw6 == 1 // copy values to other household memembers
    bysort hhid6: replace h6rural = h6rural[_n+`p'] if hhid6 == hhid6[_n+`p'] & mi(h6rural) & h6rural[_n+`p'] !=. & inw6 == 1 // copy values to other household memembers
}

***Language of Interview***
*wave 6 respondent language of interview
gen r6lang_s = .
replace r6lang_s = 1 if inlist(language,11,12,20,42)
replace r6lang_s = 2 if language == 13
replace r6lang_s = 3 if language == 14
replace r6lang_s = 4 if inlist(language,15,40)
replace r6lang_s = 5 if inlist(language,16,22)
replace r6lang_s = 6 if inlist(language,17,21,23,41)
replace r6lang_s = 7 if language == 18
replace r6lang_s = 8 if language == 19
replace r6lang_s = 9 if language == 24
replace r6lang_s = 10 if language == 25
replace r6lang_s = 11 if language == 26
replace r6lang_s = 12 if inlist(language,27,37)
replace r6lang_s = 13 if language == 28
replace r6lang_s = 14 if language == 29
replace r6lang_s = 15 if language == 30
replace r6lang_s = 16 if language == 32
replace r6lang_s = 17 if inlist(language,33,43)
replace r6lang_s = 18 if language == 34
replace r6lang_s = 19 if language == 36
replace r6lang_s = 20 if language == 39
replace r6lang_s = 21 if language == 47
replace r6lang_s = 22 if language == 31
replace r6lang_s = 23 if language == 35
replace r6lang_s = 24 if language == 38
replace r6lang_s = 25 if language == 44
replace r6lang_s = 26 if language == 45
label variable r6lang_s "r6lang_s:w6 r language of interview"
label values r6lang_s languages

*wave 6 spouse language of interview
gen s6lang_s = .
spouse r6lang_s, result(s6lang_s) wave(6)
label variable s6lang_s "s6lang_s:w6 s language of interview"
label values s6lang_s languages


***drop cover screen tracker (respondent) file raw variables
drop `demo_all_cv_r'

***drop wave 6 Asset file raw variables
drop `demo_w6_as'

***drop wave 6 Behavioural Risk file raw variables
drop `demo_w6_br'

***drop wave 6 Children file raw variables
drop `demo_w6_ch'

***drop wave 6 Consumption file raw variables
drop `demo_w6_co'

***drop wave 6 Demographic file raw variables
drop `demo_w6_dn'

***drop wave 6 Employment and Pension file raw variables
drop `demo_w6_ep'

***drop wave 6 Financial Transfer file raw variables
drop `demo_w6_ft'

***drop wave 6 Household Income file raw variables
drop `demo_w6_hh'

***drop wave 6 Housing file raw variables
drop `demo_w6_ho'

***drop wave 6 interviewer file raw variables
drop `demo_w6_iv'

***drop wave 6 Physical Health file raw variables
drop `demo_w6_ph'

***drop wave 6 Social Support file raw variables
drop `demo_w6_sp'

***drop wave 6 Dropoff file raw variables
drop `demo_w6_dropoff'

***drop wave 6 Generated Variables (Weights) file raw variables
drop `demo_w6_gv_weights'

***drop Longititudinal Weights wave 5 wave 6 file raw variables
drop `demo_long_weight_w5w6'

***drop Longititudinal Weights wave 1 - wave 6 file raw variables
drop `demo_long_weight_w1w6'

***drop wave 6 Generated Variables (Imputations) file raw variables
drop `demo_w6_imputations'

***drop wave 1 Generated Variables (ISCED) file raw variables
drop `demo_w6_isced'


******************************************************************************************



***birthmonth
reshape long r@bmonth, i(mergeid) j(wave)
egen rabmonth = mode(rbmonth), by(mergeid) minmode
label variable rabmonth "rabmonth: r birth month"
reshape wide r@bmonth, i(mergeid) j(wave)

drop r1bmonth r2bmonth r3bmonth r4bmonth r5bmonth r6bmonth

*spouse birth month
foreach w in 1 2 4 5 6 {
    gen s`w'bmonth = .
    spouse rabmonth, result(s`w'bmonth) wave(`w')
    label variable s`w'bmonth "s`w'bmonth: s birth month"
}

***age at interview in months***
*wave 1 respondent age in months
gen r1agem = .
missing_common r1iwy r1iwm rabyear rabmonth, result(r1agem)
replace r1agem = .d if rabyear==.d | rabmonth==.d
replace r1agem = .r if rabyear==.r | rabmonth==.r
replace r1agem = (r1iwy - rabyear)*12 if inrange(r1iwy,2004,2006) & inrange(rabyear,1900,2005)
replace r1agem = (r1iwm - rabmonth) + r1agem if inrange(r1iwm,1,12) & inrange(rabmonth,1,12) & inrange(r1agem,0,1440)
label variable r1agem "r1agem:w1 r age (months) at ivw"

*wave 1 spouse age in months
gen s1agem = .
missing_common r1iwy r1iwm s1byear s1bmonth, result(s1agem)
replace s1agem = .u if s1iwstat==.u
replace s1agem = .v if s1iwstat==0
replace s1agem = .d if s1byear==.d | s1bmonth==.d
replace s1agem = .r if s1byear==.r | s1bmonth==.r
replace s1agem = (r1iwy - s1byear)*12 if inrange(r1iwy,2004,2006) & inrange(s1byear,1900,2005)
replace s1agem = (r1iwm - s1bmonth) + s1agem if inrange(r1iwm,1,12) & inrange(s1bmonth,1,12) & inrange(s1agem,0,1440)
label variable s1agem "s1agem:w1 s age (months) at ivw"

***age at interview in years***
*wave 1 respondent age in years
gen r1agey = .
missing_common age_int_w1, result(r1agey)
replace r1agey = age_int_w1 if inrange(age_int_w1,14,120)
replace r1agey = .i if inrange(age_int_w1,0,13)
label variable r1agey "r1agey:w1 r age (years) at ivw"

*wave 1 spouse age in years
gen s1agey=.
missing_common s1agem, result(s1agey)
replace s1agey = .u if s1iwstat==.u
replace s1agey = .v if s1iwstat==0
replace s1agey = .d if s1byear==.d | s1bmonth==.d
replace s1agey = .r if s1byear==.r | s1bmonth==.r
replace s1agey = floor(s1agem/12) if inrange(s1agem,0,1440)
label variable s1agey "s1agey:w1 s age (years) at ivw"

drop age_int_w1

***age at interview in months***
*wave 2 respondent age in months
gen r2agem = .
missing_common r2iwy r2iwm rabyear rabmonth if interview_w2 == 1, result(r2agem)
replace r2agem = .d if rabyear==.d | rabmonth==.d
replace r2agem = .r if rabyear==.r | rabmonth==.r
replace r2agem = (r2iwy - rabyear)*12 if inrange(r2iwy,2006,2010) & inrange(rabyear,1900,2005) 
replace r2agem = (r2iwm - rabmonth) + r2agem if inrange(r2iwm,1,12) & inrange(rabmonth,1,12) & inrange(r2agem,0,1440)
label variable r2agem "r2agem:w2 r age (months) at ivw"

*wave 2 spouse age in months
gen s2agem = .
missing_common r2iwy r2iwm s2byear s2bmonth if interview_w2 == 1, result(s2agem)
replace s2agem = .u if s2iwstat==.u
replace s2agem = .v if s2iwstat==0 | s2byear==.v | s2bmonth==.v
replace s2agem = .d if s2byear==.d | s2bmonth==.d
replace s2agem = .r if s2byear==.r | s2bmonth==.r
replace s2agem = (r2iwy - s2byear)*12 if inrange(r2iwy,2006,2010) & inrange(s2byear,1900,2005)
replace s2agem = (r2iwm - s2bmonth) + s2agem if inrange(r2iwm,1,12) & inrange(s2bmonth,1,12) & inrange(s2agem,0,1440)
label variable s2agem "s2agem:w2 s age (months) at ivw"

***age at interview***
*wave 2 respondent age in years
gen r2agey = .
missing_common age_int_w2, result(r2agey)
replace r2agey = age_int_w2 if inrange(age_int_w2,0,120) & interview_w2 == 1
replace r2agey = .i if inrange(r2agey,0,13)
label variable r2agey "r2agey:w2 r age (years) at ivw"

*wave 2 spouse age in years
gen s2agey=.
missing_common s2agem, result(s2agey)
replace s2agey = .u if s2iwstat==.u
replace s2agey = .v if s2iwstat==0 | s2byear==.v | s2bmonth==.v
replace s2agey = .d if s2byear==.d | s2bmonth==.d
replace s2agey = .r if s2byear==.r | s2bmonth==.r
replace s2agey = floor(s2agem/12) if inrange(s2agem,0,1440)
replace s2agey = .i if inrange(s2agey,0,13)
label variable s2agey "s2agey:w2 s age (years) at ivw"

drop age_int_w2 interview_w2

***age at interview in months***
*wave 4 respondent age in months
gen r4agem = .
missing_common r4iwy r4iwm rabyear rabmonth, result(r4agem)
replace r4agem = .d if rabyear==.d | rabmonth==.d
replace r4agem = .r if rabyear==.r | rabmonth==.r
replace r4agem = (r4iwy - rabyear)*12 if inrange(r4iwy,2010,2012) & inrange(rabyear,1900,2005)
replace r4agem = (r4iwm - rabmonth) + r4agem if inrange(r4iwm,1,12) & inrange(rabmonth,1,12) & inrange(r4agem,0,1440)
label variable r4agem "r4agem:w4 r age (months) at ivw"

*wave 4 spouse age in months
gen s4agem = .
missing_common r4iwy r4iwm s4byear s4bmonth, result(s4agem)
replace s4agem = .u if s4iwstat==.u
replace s4agem = .v if s4iwstat==0 | s4byear==.v | s4bmonth==.v
replace s4agem = .d if s4byear==.d | s4bmonth==.d
replace s4agem = .r if s4byear==.r | s4bmonth==.r
replace s4agem = (r4iwy - s4byear)*12 if inrange(r4iwy,2010,2012) & inrange(s4byear,1900,2005)
replace s4agem = (r4iwm - s4bmonth) + s4agem if inrange(r4iwm,1,12) & inrange(s4bmonth,1,12) & inrange(s4agem,0,1440)
label variable s4agem "s4agem:w4 s age (months) at ivw"

***age at interview***
*wave 4 respondent age in years
gen r4agey = .
missing_common age_int_w4, result(r4agey)
replace r4agey = age_int_w4 if inrange(age_int_w4,0,120)
replace r4agey = .i if inrange(r4agey,0,13)
label variable r4agey "r4agey:w4 r age (years) at ivw"

*wave 4 spouse age in years
gen s4agey=.
missing_common s4agem, result(s4agey)
replace s4agey = .m if s4agem==.m
replace s4agey = .u if s4iwstat==.u
replace s4agey = .v if s4iwstat==0 | s4byear==.v | s4bmonth==.v
replace s4agey = .d if s4byear==.d | s4bmonth==.d
replace s4agey = .r if s4byear==.r | s4bmonth==.r
replace s4agey = floor(s4agem/12) if inrange(s4agem,0,1440)
replace s4agey = .i if inrange(s4agey,0,13)
label variable s4agey "s4agey:w4 s age (years) at ivw"

drop age_int_w4

***age at interview in months***
*wave 5 respondent age in months
gen r5agem = .
missing_common r5iwy r5iwm rabyear rabmonth, result(r5agem)
replace r5agem = .d if rabyear==.d | rabmonth==.d
replace r5agem = .r if rabyear==.r | rabmonth==.r
replace r5agem = (r5iwy - rabyear)*12 if r5iwy == 2013 & inrange(rabyear,1900,2005)
replace r5agem = (r5iwm - rabmonth) + r5agem if inrange(r5iwm,1,12) & inrange(rabmonth,1,12) & inrange(r5agem,0,1440)
label variable r5agem "r5agem:w5 r age (months) at ivw"

*wave 5 spouse age in months
gen s5agem = .
missing_common r5iwy r5iwm s5byear s5bmonth, result(s5agem)
replace s5agem = .u if s5iwstat==.u
replace s5agem = .v if s5iwstat==0 | s5byear==.v | s5bmonth==.v
replace s5agem = .d if s5byear==.d | s5bmonth==.d
replace s5agem = .r if s5byear==.r | s5bmonth==.r
replace s5agem = (r5iwy - s5byear)*12 if r5iwy == 2013 & inrange(s5byear,1900,2005)
replace s5agem = (r5iwm - s5bmonth) + s5agem if inrange(r5iwm,1,12) & inrange(s5bmonth,1,12) & inrange(s5agem,0,1440)
replace s5agem = .i if inrange(s5byear,2001,2013)
label variable s5agem "s5agem:w5 s age (months) at ivw"

***age at interview***
*wave 5 respondent age in years
gen r5agey = .
missing_common age_int_w5, result(r5agey)
replace r5agey = age_int_w5 if inrange(age_int_w5,0,120) & interview_w5 == 1
replace r5agey = .i if inrange(r5agey,0,13)
label variable r5agey "r5agey:w5 r age (years) at ivw"

*wave 5 spouse age in years
gen s5agey=.
missing_common s5agem, result(s5agey)
replace s5agey = .m if s5agem==.m
replace s5agey = .u if s5iwstat==.u
replace s5agey = .v if s5iwstat==0 | s5byear==.v | s5bmonth==.v
replace s5agey = .d if s5byear==.d | s5bmonth==.d
replace s5agey = .r if s5byear==.r | s5bmonth==.r
replace s5agey = floor(s5agem/12) if inrange(s5agem,0,1440)
replace s5agey = .i if inrange(s5agey,0,13) | s5agem==.i
label variable s5agey "s5agey:w5 s age (years) at ivw"

drop age_int_w5 interview_w5

***age at interview in months***
*wave 6 respondent age in months
gen r6agem = .
missing_common r6iwy r6iwm rabyear rabmonth, result(r6agem)
replace r6agem = .d if rabyear==.d | rabmonth==.d
replace r6agem = .r if rabyear==.r | rabmonth==.r
replace r6agem = (r6iwy - rabyear)*12 if r6iwy == 2015 & inrange(rabyear,1900,2005)
replace r6agem = (r6iwm - rabmonth) + r6agem if inrange(r6iwm,1,12) & inrange(rabmonth,1,12) & inrange(r6agem,0,1440)
label variable r6agem "r6agem:w6 r age (months) at ivw"

*wave 6 spouse age in months
gen s6agem = .
missing_common r6iwy r6iwm s6byear s6bmonth, result(s6agem)
replace s6agem = .u if s6iwstat==.u
replace s6agem = .v if s6iwstat==0 | s6byear==.v | s6bmonth==.v
replace s6agem = .d if s6byear==.d | s6bmonth==.d
replace s6agem = .r if s6byear==.r | s6bmonth==.r
replace s6agem = (r6iwy - s6byear)*12 if r6iwy == 2015 & inrange(s6byear,1900,2005)
replace s6agem = (r6iwm - s6bmonth) + s6agem if inrange(r6iwm,1,12) & inrange(s6bmonth,1,12) & inrange(s6agem,0,1440)
replace s6agem = .i if inrange(s6byear,2001,2013)
label variable s6agem "s6agem:w6 s age (months) at ivw"

***age at interview***
*wave 6 respondent age in years
gen r6agey = .
missing_common age_int_w6, result(r6agey)
replace r6agey = age_int_w6 if inrange(age_int_w6,0,120) & interview_w6 == 1
replace r6agey = .i if inrange(r6agey,0,13)
label variable r6agey "r6agey:w6 r age (years) at ivw"

*wave 6 spouse age in years
gen s6agey=.
missing_common s6agem, result(s6agey)
replace s6agey = .m if s6agem==.m
replace s6agey = .u if s6iwstat==.u
replace s6agey = .v if s6iwstat==0 | s6byear==.v | s6bmonth==.v
replace s6agey = .d if s6byear==.d | s6bmonth==.d
replace s6agey = .r if s6byear==.r | s6bmonth==.r
replace s6agey = floor(s6agem/12) if inrange(s6agem,0,1440)
replace s6agey = .i if inrange(s6agey,0,13)
label variable s6agey "s6agey:w6 s age (years) at ivw"

drop age_int_w6 interview_w6

***Implicate***
rename implicate implicat
label variable implicat "implicat: implicate number"

*respondent stratification variables
encode raestrat1, gen(raestrat1_)
drop raestrat1
rename raestrat1_ raestrat1
label variable raestrat1 "first stratification variable"

encode raestrat2, gen(raestrat2_)
drop raestrat2
rename raestrat2_ raestrat2
label variable raestrat2 "second stratification variable"

***Household ID***
rename hhidc hhid

replace hhid1 = "" if inw1 != 1

replace hhid2 = "" if inw2 != 1

replace hhid3 = "" if inw3 != 1

replace hhid4 = "" if inw4 != 1

replace hhid5= "" if inw5 != 1

replace hhid6= "" if inw6 != 1

***Spouse Identifier***
destring s1nmergeid, replace
format s1nmergeid %12.0f

destring s2nmergeid, replace
format s2nmergeid %12.0f

destring s3nmergeid, replace
format s3nmergeid %12.0f

destring s4nmergeid, replace
format s4nmergeid %12.0f

destring s5nmergeid, replace
format s5nmergeid %12.0f

destring s6nmergeid, replace
format s6nmergeid %12.0f

destring raspid1, replace
format raspid1 %12.0f

destring raspid2, replace
format raspid2 %12.0f

destring raspid3, replace
format raspid3 %12.0f

destring raspid4, replace
format raspid4 %12.0f

*gender for imputation
gen ragender_i = ragender

foreach w in 1 2 4 5 6 {
    gen s`w'gender_i = s`w'gender
    replace s`w'gender_i = 1 if ragender_i == 2 & inlist(r`w'mstat,1,3) & mi(s`w'gender_i)
    replace s`w'gender_i = 2 if ragender_i == 1 & inlist(r`w'mstat,1,3) & mi(s`w'gender_i)
}


*age catagories for imputation
foreach w in 1 2 4 5 6 {
    gen r`w'agey_i = r`w'agey if !mi(r`w'agey)
    gen s`w'agey_i = s`w'agey if !mi(s`w'agey)
    
    egen r`w'agecat_i = cut(r`w'agey_i), at(0,50,65,75,120) icodes
}

*benefit-unit type for imputation
foreach w in 1 2 4 5 6 {
    gen h`w'butype_i =.
    replace h`w'butype_i = 1 if inlist(r`w'mstat,1,3)
    replace h`w'butype_i = 2 if inlist(r`w'mstat,4,5,7,8) & ragender == 1
    replace h`w'butype_i = 3 if inlist(r`w'mstat,4,5,7,8) & ragender == 2
    
    *household different or same sex couple
    gen h`w'couple_type = .
    replace h`w'couple_type = 1 if h`w'butype_i == 1 & ((ragender_i == 1 & s`w'gender_i == 2) | (ragender_i == 2 & s`w'gender_i == 1))
    replace h`w'couple_type = 2 if h`w'butype_i == 1 & ((ragender_i == 1 & s`w'gender_i == 1) | (ragender_i == 2 & s`w'gender_i == 2))

    *household maximum age
    egen h`w'max_agey = rowmax(r`w'agey_i s`w'agey_i)
    
    *household age catagories for imputation
    gen h`w'agey_i = .
    replace h`w'agey_i = r`w'agey_i if inlist(h`w'butype_i,2,3)
    replace h`w'agey_i = r`w'agey_i if h`w'butype_i == 1 & h`w'couple_type == 1 & ragender_i == 1 & s`w'gender_i == 2
    replace h`w'agey_i = s`w'agey_i if h`w'butype_i == 1 & h`w'couple_type == 1 & ragender_i == 2 & s`w'gender_i == 1
    replace h`w'agey_i = r`w'agey_i if h`w'butype_i == 1 & h`w'couple_type == 1 & ragender_i == 2 & s`w'gender_i == 1 & mi(s`w'agey_i)
    replace h`w'agey_i = h`w'max_agey if h`w'butype_i == 1 & h`w'couple_type == 2 

    egen h`w'agecat_i = cut(h`w'agey_i), at(0,50,65,75,120) icodes
    
    drop r`w'agey_i 
    drop s`w'agey_i
    drop h`w'couple_type
    drop h`w'max_agey
    drop s`w'gender_i
    drop h`w'agey_i
}

foreach w in 1 2 4 5 6 {
    tab ragender_i if inw`w'==1, m
    tab r`w'agecat_i if inw`w'==1, m

    tab h`w'butype_i if inw`w'==1, m
    tab h`w'agecat_i if inw`w'==1, m
}


******************************************************************************************


*self-report of health
label define health ///
   1 "1.Excellent"  ///
   2 "2.Very good" ///
   3 "3.Good" ///
   4 "4.Fair" ///
   5 "5.Poor" ///
   .m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr" ///
   .n ".n:not asked, re-iw"

*self-report of health change
label define hlthchange ///
	1 "1.Much Better" ///
	2 "2.Somewhat Better" ///
	3 "3.About the same" ///
	4 "4.Somewhat Worse" ///
	5 "5.Much worse" ///
	.c ".c:New interview"
	
*self-report of health change flag
label define changeflag ///
	0 "0.used iw-1" ///
	1 "1.used iw-2" ///
	2 "2.used iw-4" ///
   3 "3.used iw-5" ///
	9 "9.missing"

*self-report of health question position
label define health_pos ///
	1 "1.beginning of module" ///
	2 "2.end of module" ///
	.m ".m:Missing" ///
  .d ".d:DK" ///
  .r ".r:Refuse" ///
  .u ".u:Unmar" ///
  .v ".v:Sp Nr"

*self-report of health alternative scale
label define health_alt ///
   1 "1.Very good"  ///
   2 "2.Good" ///
   3 "3.Fair" ///
   4 "4.Bad" ///
   5 "5.Very bad" ///
   .m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr"

*Whether health limits work
label define limits ///
   0 "0.Not limited"  ///
   1 "1.Limited" ///
   .m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr"

*Some difficulty with ADLs and IADLs
label define adldiff ///
   0 "0.No"  ///
   1 "1.Yes" ///
   .m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .q ".q:Not asked" ///
   .g ".g:Covered by gov ins" ///
   .n ".n:In NHM"  ///
   .v ".v:Sp Nr"

*CESD raw
label define cesdraw ///
   1 "1.almost all of the time"  ///
   2 "2.most of the time" ///
   3 "3.some of the time" ///
   4 "4.almost none of the time" ///
   .g ".g:In Vignette sample" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr" ///
   .m ".m:Missing"

*CESD yes/no
label define cesd ///
   0 "0.no"  ///
   1 "1.yes" ///
   .d ".d:DK" ///
   .r ".r:RF" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr" ///
   .s ".s:not asked wellbeing" ///
   .g ".g:In Vignette sample" ///
   .m ".m:Missing"

*EURO-D yes/no
label define heurod ///
   0 "0.no"  ///
   1 "1.yes" ///
   .d ".d:DK" ///
   .r ".r:RF" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr" ///
   .s ".s:not asked wellbeing" ///
   .m ".m:Missing" ///
   .p ".p:Proxy" 

***Health imputation flag
label define healthimpflag ///
   1 "1.no imputation" ///
   5 "5.imputation"

*Ever have health condition
label define health_problems ///
   0 "0.no"  ///
   1 "1.yes" ///
   .m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:RF" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr"

label define ehealth_problems_flag ///
   0 "0.No dispute, no change" ///
   2 "2.Disp last iw Y -> N, now yes" ///
   3 "3.Disp last iw already N, now yes" ///
   5 "5.Disp aft, Y -> N" ///
   6 "6.Disp aft, already N"

*Physical activity
label define activity ///
   2 "2.> 1 per week"  ///
   3 "3.1 per week"  ///
   4 "4.1-3 per mon"  ///
   5 "5.hardly ever or never" ///
   .m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr"

*Ever drinks
label define ever_drink ///
   0 "0.no"  ///
   1 "1.yes" ///
   .m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr"

*Days per week drinks
label define drinkx ///
	0 "0.none or less than once a month" ///
	1 "1.one to several times a month" ///
	2 "2.one to several times a week" ///
	3 "3.most days of the week" ///
	4 "4.Every day of the week" ///
	.m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr"

*Whether smokes
label define smokes ///
	0 "0.No" ///
	1 "1.Yes" ///
   .m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr" ///
   .s ".s:skipped-longitudinal"

*BMI Categorization
label define bmicate ///
	1 "1.underweight less than 18.5" ///
	2 "2.normal weight from 18.5 to 24.9" ///
	3 "3.pre-obesity from 25 to 29.9" ///
	4 "4.obesity class 1 from 30 to 34.9" ///
	5 "5.obesity class 2 from 35 to 39.9" ///
	6 "6.obesity class 3 greater than 40" ///
	.i ".i:invalid"

******************************************************************************************


*set wave number
local wv = 1

***merge with wave 1 Behavioural Risk data***
local health_w1_br br015_ br016_ br010_ br001_ br002_ br006_ br007_ br008_
merge 1:1 mergeid using "$wave_1_br", keepusing(`health_w1_br') nogen

***merge with wave 1 Mental Health data***
local health_w1_mh mh018_ mh020_ 
merge 1:1 mergeid using "$wave_1_mh", keepusing(`health_w1_mh') nogen

***merge with wave 1 Physical Health data***
local health_w1_ph ph003_ ph052_ ph002_ ph053_ ph005_ ph049d2 ph049d1 ph049d3 ///
					ph049d4 ph049d5 ph049d6 ph049d7 ph049d10 ph049d13 ph049d11 ph049d12 ///
					ph049d9 ph049d8 ph048d1 ph048d2 ph048d3 ph048d4 ph048d5 ph048d6 ///
					ph048d9 ph048d10 ph048d7 ph048d8 ph006d2 ph006d3 ph006d5 ph006d7 ph006d10 ///
					ph006d6 ph006d1 ph006d4 ph006d8 ph006d12 ph006d13 ph006d14 ph013_ ph012_ ph006d11 
merge 1:1 mergeid using "$wave_1_ph", keepusing(`health_w1_ph') nogen

***merge with wave 1 Generated Variables (Health) data***
local health_w1_gv_health euro1 euro2 euro3 euro4 euro5 euro6 euro7 ///
                          euro8 euro9 euro10 euro11 euro12 
merge 1:1 mergeid using "$wave_1_gv_health", keepusing(`health_w1_gv_health') nogen

***merge with wave 1 Dropoff data***
local health_w1_dropoff q4_a q4_b q4_c q4_d q4_e q4_h q4_j q4_g
merge 1:1 mergeid using "$wave_1_dropoff", keepusing(`health_w1_dropoff') nogen

***merge with wave 1 Vignette data***
local health_w1_vignettes type
merge 1:1 mergeid using "$wave_1_vignettes", keepusing(`health_w1_vignettes') nogen

***merge with wave 1 imputation data***
local health_w1_imput country eurod eurod_f
merge 1:1 mergeid using "$wave_1_imputations_", keepusing(`health_w1_imput') nogen


***self-report of health***
*wave 1 respondnet self-report of health
gen r1shlt =.
missing_common ph003_ ph052_, result(r1shlt)
replace r1shlt = 1 if ph003_==1 | ph052_==1
replace r1shlt = 2 if ph003_==2 | ph052_==2
replace r1shlt = 3 if ph003_==3 | ph052_==3
replace r1shlt = 4 if ph003_==4 | ph052_==4
replace r1shlt = 5 if ph003_==5 | ph052_==5
label variable r1shlt "r1shlt:w1 r self-report of health"

*wave 1 spouse self-report of health
gen s1shlt =.
spouse r1shlt, result(s1shlt) wave(1)
label variable s1shlt "s1shlt:w1 s self-report of health"

*wave 1 respondent position flag for self-report of health
gen r1shltf =.
replace r1shltf = .m if (missing(ph003_) | missing(ph052_)) & inw`wv' == 1
replace r1shltf = 1 if inrange(ph003_,1,5)
replace r1shltf = 2 if inrange(ph052_,1,5)
label variable r1shltf "r1shltf:w1 r flag position of self-report health"

*wave 1 spouse position flag for self-report of health
gen s1shltf =.
spouse r1shltf, result(s1shltf) wave(1)
label variable s1shltf "s1shltf:w1 s flag position of self-report health"

*value labels
label values r1shlt health
label values s1shlt health
label values r1shltf health_pos
label values s1shltf health_pos

***self-report of health alternative scale(EU)***
*wave 1 respondnet self-report of health alternative scale
gen r1shlta =.
missing_common ph002_ ph053_, result(r1shlta)
replace r1shlta = .m if (missing(ph002_) | missing(ph053_)) & inw`wv' == 1
replace r1shlta =1  if ph002_==1 | ph053_==1
replace r1shlta =2  if ph002_==2 | ph053_==2
replace r1shlta =3  if ph002_==3 | ph053_==3
replace r1shlta =4  if ph002_==4 | ph053_==4
replace r1shlta =5  if ph002_==5 | ph053_==5
label variable r1shlta "r1shlta:w1 r self-report of health alt"

*wave 1 spouse self-report of health alternative scale
gen s1shlta =.
spouse r1shlta, result(s1shlta) wave(1)
label variable s1shlta "s1shlta:w1 s self-report of health alt"

*wave 1 respondent position flag for self-report of health alt
gen r1shltaf =.
replace r1shltaf = .m if (missing(ph002_) | missing(ph053_)) & inw`wv' == 1
replace r1shltaf = 1 if inrange(ph002_,1,5)
replace r1shltaf = 2 if inrange(ph053_,1,5)
label variable r1shltaf "r1shltaf:w1 r flag position of self-report health alt"

*wave 1 spouse position flag for self-report of health alt
gen s1shltaf =.
spouse r1shltaf, result(s1shltaf) wave(1)
label variable s1shltaf "s1shltaf:w1 s flag position of self-report health alt"

*value labels
label values r1shlta health_alt
label values s1shlta health_alt
label values r1shltaf health_pos
label values s1shltaf health_pos

***Whether health limits activities***
*wave 1 respodnet whether health limits activities
gen r1hlthlma =.
missing_common ph005_, result(r1hlthlma)
replace r1hlthlma = 0 if ph005_ == 3
replace r1hlthlma = 1 if inrange(ph005_,1,2)
label variable r1hlthlma "r1hlthlma:w1 r hlth problem limits activities"

*wave 1 spouse whether healh limits activities
gen s1hlthlma =.
spouse r1hlthlma, result(s1hlthlma) wave(1)
label variable s1hlthlma "s1hlthlma:w1 s hlth problem limits activities"

*value labels
label values r1hlthlma limits
label values s1hlthlma limits

***Some difficulty walking cross room***
*wave 1 respondent difficulty walking across room
gen r1walkra =.
missing_common ph049d2, result(r1walkra)	
replace r1walkra = 0 if ph049d2==0 
replace r1walkra = 1 if ph049d2==1 
label variable r1walkra "r1walkra:w1 R Some Diff-Walk across room"

*wave 1 spouse difficulty walking across room
gen s1walkra =.
spouse r1walkra, result(s1walkra) wave(1)
label variable s1walkra "s1walkra:w1 S Some Diff-Walk across room"

*value labels
label values r1walkra adldiff
label values s1walkra adldiff

***Some difficulty dressing***
*wave 1 respondent difficulty dressing
gen r1dressa =.
missing_common ph049d1 , result(r1dressa)		
replace r1dressa = 0 if ph049d1==0 
replace r1dressa = 1 if ph049d1==1 
label variable r1dressa "r1dressa:w1 R Some Diff-Dressing"

*wave 1 spouse difficulty dressing
gen s1dressa =.
spouse r1dressa, result(s1dressa) wave(1)
label variable s1dressa "s1dressa:w1 S Some Diff-Dressing"

*value labels
label values r1dressa adldiff
label values s1dressa adldiff

***Some difficulty bathing, shower***
*wave 1 respondent difficulty bathing, shower
gen r1batha =.
missing_common ph049d3 , result(r1batha)		
replace r1batha = 0 if ph049d3==0 
replace r1batha = 1 if ph049d3==1 
label variable r1batha "r1batha:w1 R Some Diff-Bathing, shower"

*wave 1 spouse difficulty bathing, shower
gen s1batha =.
spouse r1batha, result(s1batha) wave(1)
label variable s1batha "s1batha:w1 S Some Diff-Bathing, shower"

*value labels
label values r1batha adldiff
label values s1batha adldiff

***Some difficulty eating***
*wave 1 respondent difficulty eating
gen r1eata =.
missing_common ph049d4 , result(r1eata)
replace r1eata = 0 if ph049d4==0 
replace r1eata = 1 if ph049d4==1 
label variable r1eata "r1eata:w1 R Some Diff-Eating"

*wave 1 spouse difficulty eating
gen s1eata =.
spouse r1eata, result(s1eata) wave(1)
label variable s1eata "s1eata:w1 S Some Diff-Eating"

*value labels
label values r1eata adldiff
label values s1eata adldiff

***Some difficulty getting in/out of bed***
*wave 1 respondent difficulty getting in/out of bed
gen r1beda =.
missing_common ph049d5 , result(r1beda)
replace r1beda = 0 if ph049d5==0  
replace r1beda = 1 if ph049d5==1 
label variable r1beda "r1beda:w1 R Some Diff-Get in/out bed"

*wave 1 spouse difficulty getting in/out of bed
gen s1beda =.
spouse r1beda, result(s1beda) wave(1)
label variable s1beda "s1beda:w1 S Some Diff-Get in/out bed"

*value labels
label values r1beda adldiff
label values s1beda adldiff

***Some difficulty using the toilet***
*wave 1 respondent difficulty using the toilet
gen r1toilta =.
missing_common ph049d6 , result(r1toilta)			
replace r1toilta = 0 if ph049d6==0  
replace r1toilta = 1 if ph049d6==1 
label variable r1toilta "r1toilta:w1 R Some Diff-Using the toilet"

*wave 1 spouse difficulty using the toilet
gen s1toilta =.
spouse r1toilta, result(s1toilta) wave(1)
label variable s1toilta "s1toilta:w1 S Some Diff-Using the toilet"

*value labels
label values r1toilta adldiff
label values s1toilta adldiff

***Some difficulty using using a map***
*wave 1 respondent difficulty using a map
gen r1mapa =.
missing_common ph049d7 , result(r1mapa)	
replace r1mapa = 0 if ph049d7==0 
replace r1mapa = 1 if ph049d7==1  
label variable r1mapa "r1mapa:w1 R Some Diff-Use a map"

*wave 1 spouse difficulty using a map
gen s1mapa =.
spouse r1mapa, result(s1mapa) wave(1)
label variable s1mapa "s1mapa:w1 S Some Diff-Use a map"

*value labels
label values r1mapa adldiff
label values s1mapa adldiff

***Some difficulty using a telephone***
*wave 1 respondent difficulty using a telephone
gen r1phonea =.
missing_common ph049d10, result(r1phonea)			
replace r1phonea = 0 if ph049d10==0 
replace r1phonea = 1 if ph049d10==1 
label variable r1phonea "r1phonea:w1 R Some Diff-Use telephone"

*wave 1 spouse difficulty using a telephone
gen s1phonea =.
spouse r1phonea, result(s1phonea) wave(1)
label variable s1phonea "s1phonea:w1 S Some Diff-Use a telephone"

*value labels
label values r1phonea adldiff
label values s1phonea adldiff

***Some difficulty manging money***
*wave 1 respondent difficulty manging money
gen r1moneya =.
missing_common ph049d13 , result(r1moneya)				
replace r1moneya = 0 if ph049d13==0 
replace r1moneya = 1 if ph049d13==1
label variable r1moneya "r1moneya:w1 R Some Diff-Managing money"

*wave 1 spouse difficulty manging money
gen s1moneya =.
spouse r1moneya, result(s1moneya) wave(1)
label variable s1moneya "s1moneya:w1 S Some Diff-Managing money"

*value labels
label values r1moneya adldiff
label values s1moneya adldiff

***Some difficulty taking medications***
*wave 1 respondent difficulty taking medications
gen r1medsa =.
missing_common ph049d11 , result(r1medsa)					
replace r1medsa = 0 if ph049d11==0 
replace r1medsa = 1 if ph049d11==1 
label variable r1medsa "r1medsa:w1 R Some Diff-Take medications"

*wave 1 spouse difficulty taking medications
gen s1medsa =.
spouse r1medsa, result(s1medsa) wave(1)
label variable s1medsa "s1medsa:w1 S Some Diff-Take medications"

*value labels
label values r1medsa adldiff
label values s1medsa adldiff

***Some difficulty shopping for groceries***
*wave 1 respondent difficulty shopping for groceries
gen r1shopa =.
missing_common ph049d9, result(r1shopa)			
replace r1shopa = 0 if ph049d9==0 
replace r1shopa = 1 if ph049d9==1 
label variable r1shopa "r1shopa:w1 R Some Diff-Shop for grocery"

*wave 1 spouse difficulty shopping for groceries
gen s1shopa =.
spouse r1shopa, result(s1shopa) wave(1)
label variable s1shopa "s1shopa:w1 S Some Diff-Shop for grocery"

*value labels
label values r1shopa adldiff
label values s1shopa adldiff

***Some difficulty preparing a hot meal***
*wave 1 respondent difficulty preparing a hot meal
gen r1mealsa =.
missing_common ph049d8 , result(r1mealsa)
replace r1mealsa = 0 if ph049d8==0  
replace r1mealsa = 1 if ph049d8==1 
label variable r1mealsa "r1mealsa:w1 R Some Diff-Prepare hot meal"

*wave 1 spouse difficulty preparing a hot meal
gen s1mealsa =.
spouse r1mealsa, result(s1mealsa) wave(1)
label variable s1mealsa "s1mealsa:w1 S Some Diff-Prepare hot meal"

*value labels
label values r1mealsa adldiff
label values s1mealsa adldiff

***Some difficulty doing work around the house***
*wave 1 respondent difficulty doing work around the house
gen r1housewka = .
missing_common ph049d12, result(r1housewka)
replace r1housewka = 0 if ph049d12==0
replace r1housewka = 1 if ph049d12==1
label variable r1housewka "r1housewka:w1 R some diff-doing work around house"
label values r1housewka adldiff

*wave 1 spouse difficulty doing work around the house
gen s1housewka = .
spouse r1housewka, result(s1housewka) wave(1)
label variable s1housewka "s1housewka:w1 s some diff-doing work around house"
label values s1housewka adldiff

***Difficulty Walking 100m***
*Wave 1 respondent difficulty walking 100m
gen r1walk100a =.
missing_common ph048d1 , result(r1walk100a)
replace r1walk100a = 0 if ph048d1==0  
replace r1walk100a = 1 if ph048d1==1 
label variable r1walk100a "r1walk100a:w1 R Some Diff-Walk 100m"

*wave 1 spouse difficulty walking 100m
gen s1walk100a =.
spouse r1walk100a, result(s1walk100a) wave(1)
label variable s1walk100a "s1walk100a:w1 S Some Diff-Walk 100m"

*value labels
label values r1walk100a adldiff
label values s1walk100a adldiff

***Difficulty sitting for 2 hours***
*Wave 1 respondent difficulty sitting for 2 hours
gen r1sita =.
missing_common ph048d2 , result(r1sita)
replace r1sita = 0 if ph048d2==0
replace r1sita = 1 if ph048d2==1 
label variable r1sita "r1sita:w1 R Some Diff-Sit for 2 hours"

*wave 1 spouse difficulty sitting for 2 hours
gen s1sita =.
spouse r1sita, result(s1sita) wave(1)
label variable s1sita "s1sita:w1 S Some Diff-Sit for 2 hours"

*value labels
label values r1sita adldiff
label values s1sita adldiff

***Difficulty getting up from a chair***
*Wave 1 respondent difficulty getting up from a chair
gen r1chaira =.
missing_common ph048d3  , result(r1chaira)	
replace r1chaira = 0 if ph048d3==0 
replace r1chaira = 1 if ph048d3==1
label variable r1chaira "r1chaira:w1 R Some Diff-Get up fr chair"

*wave 1 spouse difficulty getting up from a chair
gen s1chaira =.
spouse r1chaira, result(s1chaira) wave(1)
label variable s1chaira "s1chaira:w1 S Some Diff-Get up fr chair"

*value labels
label values r1chaira adldiff
label values s1chaira adldiff

***Difficulty climbing several flights of stairs***
*Wave 1 respondent difficulty climbing several flights of stairs
gen r1climsa =.
missing_common ph048d4 , result(r1climsa)
replace r1climsa = 0 if ph048d4==0 
replace r1climsa = 1 if ph048d4==1 
label variable r1climsa "r1climsa:w1 R Some Diff-Clmb sev flt str"

*wave 1 spouse difficulty climbing several flights of stairs
gen s1climsa =.
spouse r1climsa, result(s1climsa) wave(1)
label variable s1climsa "s1climsa:w1 S Some Diff-Clmb sev flt str"

*value labels
label values r1climsa adldiff
label values s1climsa adldiff

***Difficulty climbing one flight of stairs***
*Wave 1 respondent difficulty climbing one flight of stairs
gen r1clim1a =.
missing_common ph048d5 , result(r1clim1a)		
replace r1clim1a = 0 if ph048d5==0 
replace r1clim1a = 1 if ph048d5==1
label variable r1clim1a "r1clim1a:w1 R Some Diff-Clmb 1 flt str"

*wave 1 spouse difficulty climbing one flight of stairs
gen s1clim1a =.
spouse r1clim1a, result(s1clim1a) wave(1)
label variable s1clim1a "s1clim1a:w1 S Some Diff-Clmb 1 flt str"

*value labels
label values r1clim1a adldiff
label values s1clim1a adldiff

***Difficulty stooping/keeling/crouching***
*Wave 1 respondent difficulty stooping/keeling/crouching
gen r1stoopa =.
missing_common ph048d6, result(r1stoopa)		
replace r1stoopa = 0 if ph048d6==0
replace r1stoopa = 1 if ph048d6==1 
label variable r1stoopa "r1stoopa:w1 R Some Diff-Stoop/Kneel/Crch"

*wave 1 spouse difficulty stooping/keeling/crouching
gen s1stoopa =.
spouse r1stoopa, result(s1stoopa) wave(1)
label variable s1stoopa "s1stoopa:w1 S Some Diff-Stoop/Kneel/Crch"

*value labels
label values r1stoopa adldiff
label values s1stoopa adldiff

***Difficulty lifting/carrying 10lbs***
*Wave 1 respondent difficulty lifting/carrying 10lbs
gen r1lifta =.
missing_common ph048d9 , result(r1lifta)	
replace r1lifta = 0 if ph048d9==0
replace r1lifta = 1 if ph048d9==1
label variable r1lifta "r1lifta:w1 R Some Diff-Lift/carry 10lbs"

*wave 1 spouse difficulty lifting/carrying 10lbs
gen s1lifta =.
spouse r1lifta, result(s1lifta) wave(1)
label variable s1lifta "s1lifta:w1 S Some Diff-Lift/carry 10lbs"

*value labels
label values r1lifta adldiff
label values s1lifta adldiff

***Difficulty picking up a small coin***
*Wave 1 respondent difficulty picking up a small coin
gen r1dimea =.
missing_common ph048d10 , result(r1dimea)
replace r1dimea = 0 if ph048d10==0 
replace r1dimea = 1 if ph048d10==1 

label variable r1dimea "r1dimea:w1 R Some Diff-Pick up a small coin"

*wave 1 spouse difficulty picking up a small coin
gen s1dimea =.
spouse r1dimea, result(s1dimea) wave(1)
label variable s1dimea "s1dimea:w1 S Some Diff-Pick up a small coin"

*value labels
label values r1dimea adldiff
label values s1dimea adldiff

***Difficulty reaching/extending arms up***
*Wave 1 respondent difficulty reaching/extending arms up
gen r1armsa =.
missing_common ph048d7, result(r1armsa)
replace r1armsa = 0 if ph048d7==0
replace r1armsa = 1 if ph048d7==1
label variable r1armsa "r1armsa:w1 R Some Diff-Rch/xtnd arms up"

*wave 1 spouse difficulty reaching/extending arms up
gen s1armsa =.
spouse r1armsa, result(s1armsa) wave(1)
label variable s1armsa "s1armsa:w1 S Some Diff-Rch/xtnd arms up"

*value labels
label values r1armsa adldiff
label values s1armsa adldiff

***Difficulty pushing/pulling a large object***
*Wave 1 respondent difficulty pushing/pulling a large object
gen r1pusha =.
missing_common ph048d8, result(r1pusha)
replace r1pusha = 0 if ph048d8==0
replace r1pusha = 1 if ph048d8==1
label variable r1pusha "r1pusha:w1 R Some Diff-Push/pull lg obj"

*wave 1 spouse difficulty pushing/pulling a large object
gen s1pusha =.
spouse r1pusha, result(s1pusha) wave(1)
label variable s1pusha "s1pusha:w1 S Some Diff-Push/pull lg obj"

*value labels
label values r1pusha adldiff
label values s1pusha adldiff

***ADL Summary 0-5***
*Wave 1 respondent ADL number of missings
egen r1adlam = rowmiss(r1batha r1dressa r1eata r1beda r1walkra) if inw1 == 1
label variable r1adlam "r1adlam:w1 Missings in ADL summary"

*wave 1 spouse ADL number of missings
gen s1adlam =.
spouse r1adlam, result(s1adlam) wave(1)
label variable s1adlam "s1adlam:w1 Missings in ADL summary"

local adl
foreach v of varlist r1batha r1dressa r1eata r1beda r1walkra {
	recode `v' (miss = 0), generate(`v'_m)
	local adl `adl' `v'_m
}

*Wave 1 respondent ADL Summary 0-5
gen r1adla =.
missing_h r1batha r1dressa r1eata r1beda r1walkra, result(r1adla)
replace r1adla = r1batha_m + r1dressa_m + r1eata_m + r1beda_m + r1walkra_m if inrange(r1adlam,0,4)
label variable r1adla "r1adla:w1 R Some Diff-ADLs /0-5"

*wave 1 spouse ADL Summary 0-5
gen s1adla =.
spouse r1adla, result(s1adla) wave(1)
label variable s1adla "s1adla:w1 S Some Diff-ADLs /0-5"

*drop intermediate variables
drop `adl'

***ADL Wallace Summary 0-3***
*Wave 1 respondent ADL Wallace number of missings
egen r1adlwam = rowmiss(r1batha r1dressa r1eata) if inw1 == 1
label variable r1adlwam "r1adlwam:w1 r Missings in ADL Wallace summary"

*wave 1 spouse ADL Wallace number of missings
gen s1adlwam =.
spouse r1adlwam, result(s1adlwam) wave(1)
label variable s1adlwam "s1adlwam:w1 s Missings in ADL Wallace summary"

local adlw
foreach v of varlist r1batha r1dressa r1eata {
	recode `v' (miss = 0), generate(`v'_m)
	local adlw `adlw' `v'_m
}

*Wave 1 respondent ADL Wallace Summary 0-3
gen r1adlwa =.
missing_h r1batha r1dressa r1eata, result(r1adlwa)
replace r1adlwa = r1batha_m + r1dressa_m + r1eata_m if inrange(r1adlwam,0,2)
label variable r1adlwa "r1adlwa:w1 R Some Diff-ADLs:Wallace /0-3"

*wave 1 spouse ADL Wallace Summary 0-3
gen s1adlwa =.
spouse r1adlwa, result(s1adlwa) wave(1)
label variable s1adlwa "s1adlwa:w1 S Some Diff-ADLs:Wallace /0-3"

*drop intermediate variables
drop `adlw'

***IADL Summary 0-3***
*Wave 1 respondent IADL number of missings
egen r1iadlam = rowmiss(r1phonea r1moneya r1medsa) if inw1 == 1
label variable r1iadlam "r1iadlam:w1 r Missings in IADL 0-3 summary"

*wave 1 spouse IADL number of missings
gen s1iadlam =.
spouse r1iadlam, result(s1iadlam) wave(1)
label variable s1iadlam "s1iadlam:w1 s Missings in IADL 0-3 summary"

local iadl
foreach v of varlist r1phonea r1moneya r1medsa {
	recode `v' (miss = 0), generate(`v'_m)
	local iadl `iadl' `v'_m
} 

*Wave 1 respondent IADLs Summary 0-3
gen r1iadla =.
missing_h r1phonea r1moneya r1medsa, result(r1iadla)
replace r1iadla = r1phonea_m + r1moneya_m + r1medsa_m if inrange(r1iadlam,0,2)
label variable r1iadla "r1iadla:w1 R Some Diff-IADLs: /0-3"

*wave 1 spouse IADLs Summary 0-3
gen s1iadla =.
spouse r1iadla, result(s1iadla) wave(1)
label variable s1iadla "s1iadla:w1 S Some Diff-IADLs: /0-3"

*drop intermediate variables
drop `iadl'

***IADL Summary 0-5***
*Wave 1 respondent IADL number of missings
egen r1iadlzam = rowmiss(r1phonea r1moneya r1medsa r1shopa r1mealsa) if inw1 == 1
label variable r1iadlzam "r1iadlzam:w1 r Missings in IADL 0-5 summary"

*wave 1 spouse IADL number of missings
gen s1iadlzam =.
spouse r1iadlzam, result(s1iadlzam) wave(1)
label variable s1iadlzam "s1iadlzam:w1 s Missings in IADL 0-5 summary"

local iadlz
foreach v of varlist r1phonea r1moneya r1medsa r1shopa r1mealsa {
	recode `v' (miss = 0), generate(`v'_m)
	local iadlz `iadlz' `v'_m
} 

*Wave 1 respondent IADLs Summary 0-5
gen r1iadlza =.
missing_h r1phonea r1moneya r1medsa r1shopa r1mealsa, result(r1iadlza)
replace r1iadlza = r1phonea_m + r1moneya_m + r1medsa_m + r1shopa_m + r1mealsa_m if inrange(r1iadlzam,0,4)
label variable r1iadlza "r1iadlza:w1 R Some Diff-IADLs: /0-5"

*wave 1 spouse IADLs Summary 0-5
gen s1iadlza =.
spouse r1iadlza, result(s1iadlza) wave(1)
label variable s1iadlza "s1iadlza:w1 S Some Diff-IADLs: /0-5"

*drop intermediate variables
drop `iadlz'

***Mobility Summary***
*Wave 1 respondent Mobility Summary number of missings
egen r1mobilbm = rowmiss(r1walk100a r1walkra r1climsa r1clim1a) if inw1 == 1
label variable r1mobilbm "r1mobilbm:w1 r Missings in mobility summary"

*wave 1 spouse Mobility Summary number of missings
gen s1mobilbm =.
spouse r1mobilbm, result(s1mobilbm) wave(1)
label variable s1mobilbm "s1mobilbm:w1 s Missings in mobility summary"

local mobilb
foreach v of varlist r1walk100a r1walkra r1climsa r1clim1a {
	recode `v' (miss = 0), generate(`v'_m)
	local mobilb `mobilb' `v'_m
} 

*Wave 1 respondent Mobility Summary
gen r1mobilb =.
missing_h r1walk100a r1walkra r1climsa r1clim1a, result(r1mobilb)
replace r1mobilb = r1walk100a_m + r1walkra_m + r1climsa_m + r1clim1a_m if inrange(r1mobilbm,0,3)
label variable r1mobilb "r1mobilb:w1 R Some Diff-Mobility /0-4"

*wave 1 spouse Mobility Summary
gen s1mobilb =.
spouse r1mobilb, result(s1mobilb) wave(1)
label variable s1mobilb "s1mobilb:w1 S Some Diff-Mobility /0-4"

*drop intermediate variables
drop `mobilb'

***Large Muscle Summary***
*Wave 1 respondent Large Muscle Summary number of missings
egen r1lgmusam = rowmiss(r1sita r1chaira r1stoopa r1pusha) if inw1 == 1
label variable r1lgmusam "r1lgmusam:w1 r Missings in large muscle summary"

*wave 1 spouse Large Muscle Summary number of missings
gen s1lgmusam =.
spouse r1lgmusam, result(s1lgmusam) wave(1)
label variable s1lgmusam "s1lgmusam:w1 s Missings in large muscle summary"

local lgmusa
foreach v of varlist r1sita r1chaira r1stoopa r1pusha {
	recode `v' (miss = 0), generate(`v'_m)
	local lgmusa `lgmusa' `v'_m
} 

*Wave 1 respondent Large Muscle Summary
gen r1lgmusa =.
missing_h r1sita r1chaira r1stoopa r1pusha, result(r1lgmusa)
replace r1lgmusa = r1sita_m + r1chaira_m + r1stoopa_m + r1pusha_m if inrange(r1lgmusam,0,3)
label variable r1lgmusa "r1lgmusa:w1 R Some Diff-Large muscle /0-4"

*wave 1 spouse Large Muscle Summary
gen s1lgmusa =.
spouse r1lgmusa, result(s1lgmusa) wave(1)
label variable s1lgmusa "s1lgmusa:w1 S Some Diff-Large muscle /0-4"

*drop intermediate variables
drop `lgmusa'

***Gross Motor Summary***
*Wave 1 respondent Gross Motor Summary number of missings
egen r1grossam = rowmiss(r1walk100a r1walkra r1clim1a r1beda r1batha) if inw1 == 1
label variable r1grossam "r1grossam:w1 r Missings in wk,rn,clmb,bd,bth summary"

*wave 1 spouse Gross Motor Summary number of missings
gen s1grossam =.
spouse r1grossam, result(s1grossam) wave(1)
label variable s1grossam "s1grossam:w1 s Missings in wk,rn,clmb,bd,bth summary"

local grossa
foreach v of varlist r1walk100a r1walkra r1clim1a r1beda r1batha {
	recode `v' (miss = 0), generate(`v'_m)
	local grossa `grossa' `v'_m
}

*Wave 1 respondent Gross Motor Summary
gen r1grossa =.
missing_h r1walk100a r1walkra r1clim1a r1beda r1batha, result(r1grossa)
replace r1grossa = r1walk100a_m + r1walkra_m + r1clim1a_m + r1beda_m + r1batha_m if inrange(r1grossam,0,4)
label variable r1grossa "r1grossa:w1 R Some Diff-Wk,rn,clmb,bd,bth /0-5"

*wave 1 spouse Gross Motor Summary
gen s1grossa =.
spouse r1grossa, result(s1grossa) wave(1)
label variable s1grossa "s1grossa:w1 S Some Diff-Wk,rn,clmb,bd,bth /0-5"

*drop intermediate variables
drop `grossa'

***Fine Motor Summary***
*Wave 1 respondent Fine Motor Summary number of missings
egen r1fineam = rowmiss(r1dimea r1eata r1dressa) if inw1 == 1
label variable r1fineam "r1fineam:w1 r Missings in dime,eat,dress summary"

*wave 1 spouse Gross Fine Summary number of missings
gen s1fineam =.
spouse r1fineam, result(s1fineam) wave(1)
label variable s1fineam "s1fineam:w1 s Missings in dime,eat,dress summary"

local finea
foreach v of varlist r1dimea r1eata r1dressa {
	recode `v' (miss = 0), generate(`v'_m)
	local finea `finea' `v'_m
}

*Wave 1 respondent Fine Motor Summary
gen r1finea =.
missing_h r1dimea r1eata r1dressa, result(r1finea)
replace r1finea = r1dimea_m + r1eata_m + r1dressa_m if inrange(r1fineam,0,2)
label variable r1finea "r1finea:w1 R Some Diff-dime,eat,dress /0-3"

*wave 1 spouse Fine Motor Summary
gen s1finea =.
spouse r1finea, result(s1finea) wave(1)
label variable s1finea "s1finea:w1 S Some Diff-dime,eat,dress /0-3"

*drop intermediate variables
drop `finea'

***CESD: Felt Depressed*****
*Wave 1 respondent Felt Depressed, by time
gen r1deprex =. 
replace r1deprex = .m if (q4_a ==-5 | missing(q4_a)) & inw`wv' == 1
replace r1deprex = .g if inlist(type,"A","B")
replace r1deprex = q4_a if inrange(q4_a,1,4)
label variable r1deprex "r1deprex:w1 CESD: r Felt depressed, by time (1-4)"

*wave 1 spouse Felt Depressed, by time
gen s1deprex =.
spouse r1deprex, result(s1deprex) wave(1)
label variable s1deprex "s1deprex:w1 CESD: s Felt depressed, by time (1-4)"

*value labels
label values r1deprex cesdraw
label values s1deprex cesdraw

***CESD: Everything is an effort***
*Wave 1 respondent Everything is an effort, by time
gen r1efforx =.
replace r1efforx = .m if (q4_b ==-5 | missing(q4_b)) & inw`wv' == 1
replace r1efforx = .g if inlist(type,"A","B")
replace r1efforx = q4_b if inrange(q4_b,1,4)
label variable r1efforx "r1efforx:w1 CESD: r Everything an effort, by time (1-4)"

*wave 1 spouse Everything is an effort, by time
gen s1efforx =.
spouse r1efforx, result(s1efforx) wave(1)
label variable s1efforx "s1efforx:w1 CESD: s Everything an effort, by time (1-4)"

*value labels
label values r1efforx cesdraw
label values s1efforx cesdraw

***CESD: Sleep was restless***
*Wave 1 respondent Sleep was restless, by time
gen r1sleepx =.
replace r1sleepx = .m if (q4_c ==-5 | missing(q4_c)) & inw`wv' == 1
replace r1sleepx = .g if inlist(type,"A","B")
replace r1sleepx = q4_c if inrange(q4_c,1,4)
label variable r1sleepx "r1sleepx:w1 CESD: r Sleep was restless, by time (1-4)"

*wave 1 spouse Sleep was restless, by time
gen s1sleepx =.
spouse r1sleepx, result(s1sleepx) wave(1)
label variable s1sleepx "s1sleepx:w1 CESD: s Sleep was restless, by time (1-4)"

*value labels
label values r1sleepx cesdraw
label values s1sleepx cesdraw

***CESD: Was happy*****
*Wave 1 respondent Was happy, by time
gen r1whappx =.
replace r1whappx = .m if (q4_d ==-5 | missing(q4_d)) & inw`wv' == 1
replace r1whappx = .g if inlist(type,"A","B")
replace r1whappx = q4_d if inrange(q4_d,1,4)
label variable r1whappx "r1whappx:w1 CESD: r Was happy, by time (1-4)"

*wave 1 spouse Was happy, by time
gen s1whappx =.
spouse r1whappx, result(s1whappx) wave(1)
label variable s1whappx "s1whappx:w1 CESD: s Was happy, by time (1-4)"

*value labels
label values r1whappx cesdraw
label values s1whappx cesdraw

***CESD: Felt lonely***
*Wave 1 respondent Felt lonely, by time
gen r1flonex =.
replace r1flonex = .m if (q4_e ==-5 | missing(q4_e)) & inw`wv' == 1
replace r1flonex = .g if inlist(type,"A","B")
replace r1flonex = q4_e if inrange(q4_e,1,4)
label variable r1flonex "r1flonex:w1 CESD: r Felt lonely, by time (1-4)"

*wave 1 spouse Felt lonely, by time
gen s1flonex =.
spouse r1flonex, result(s1flonex) wave(1)
label variable s1flonex "s1flonex:w1 CESD: s Felt lonely, by time (1-4)"

*value labels
label values r1flonex cesdraw
label values s1flonex cesdraw

***CESD: Felt sad***
*Wave 1 respondent Felt sad, by time
gen r1fsadx =.
replace r1fsadx = .m if (q4_h ==-5 | missing(q4_h)) & inw`wv' == 1
replace r1fsadx = .g if inlist(type,"A","B")
replace r1fsadx = q4_h if inrange(q4_h,1,4)
label variable r1fsadx "r1fsadx:w1 CESD: r Felt sad, by time (1-4)"

*wave 1 spouse Felt sad, by time
gen s1fsadx =.
spouse r1fsadx, result(s1fsadx) wave(1)
label variable s1fsadx "s1fsadx:w1 CESD: s Felt sad, by time (1-4)"

*value labels
label values r1fsadx cesdraw
label values s1fsadx cesdraw

***CESD: Could not get going***
*Wave 1 respondent Could not get going, by time
gen r1goingx =.
replace r1goingx = .m if (q4_j ==-5 | missing(q4_j)) & inw`wv' == 1
replace r1goingx = .g if inlist(type,"A","B")
replace r1goingx = q4_j if inrange(q4_j,1,4)
label variable r1goingx "r1goingx:w1 CESD: r Could not get going, by time (1-4)"

*wave 1 spouse Could not get going, by time
gen s1goingx =.
spouse r1goingx, result(s1goingx) wave(1)
label variable s1goingx "s1goingx:w1 CESD: s Could not get going, by time (1-4)"

*value labels
label values r1goingx cesdraw
label values s1goingx cesdraw

***CESD: Enjoyed life***
*Wave 1 respondent Enjoyed life, by time
gen r1enlifx =.
replace r1enlifx = .m if (q4_g ==-5 | missing(q4_g)) & inw`wv' == 1
replace r1enlifx = .g if inlist(type,"A","B")
replace r1enlifx = q4_g if inrange(q4_g,1,4)
label variable r1enlifx "r1enlifx:w1 CESD: r Enjoyed life, by time (1-4)"

*wave 1 spouse Enjoyed life, by time
gen s1enlifx =.
spouse r1enlifx, result(s1enlifx) wave(1)
label variable s1enlifx "s1enlifx:w1 CESD: s Enjoyed life, by time (1-4)"

*value labels
label values r1enlifx cesdraw
label values s1enlifx cesdraw

***EURO-D: Depression***
*Wave 1 respondent depression
gen r1depress =.
missing_common euro1, result(r1depress)
replace r1depress = euro1 if inlist(euro1,0,1)
label variable r1depress "r1depress:w1 EURO-D: r Depression (0,1)"
label values r1depress heurod

*wave 1 spouse depression
gen s1depress =.
spouse r1depress, result(s1depress) wave(1)
label variable s1depress "s1depress:w1 EURO-D: s Depression (0,1)"
label values s1depress heurod

***EURO-D: Pessimism***
*Wave 1 respondent pessimism
gen r1pessim =.
missing_common euro2, result(r1pessim)
replace r1pessim = euro2 if inlist(euro2,0,1)
label variable r1pessim "r1pessim:w1 EURO-D: r Pessimism (0,1)"
label values r1pessim heurod

*wave 1 spouse pessimism
gen s1pessim =.
spouse r1pessim, result(s1pessim) wave(1)
label variable s1pessim "s1pessim:w1 EURO-D: s Pessimism (0,1)"
label values s1pessim heurod

***EURO-D: Suicidality***
*Wave 1 respondent suicidality
gen r1suicid =.
missing_common euro3, result(r1suicid)
replace r1suicid = euro3 if inlist(euro3,0,1)
label variable r1suicid "r1suicid:w1 EURO-D: r Suicidality (0,1)"
label values r1suicid heurod

*wave 1 spouse suicidality
gen s1suicid =.
spouse r1suicid, result(s1suicid) wave(1)
label variable s1suicid "s1suicid:w1 EURO-D: s Suicidality (0,1)"
label values s1suicid heurod

***EURO-D: Guilt***
*Wave 1 respondent guilt
gen r1guilt =.
missing_common euro4, result(r1guilt)
replace r1guilt = euro4 if inlist(euro4,0,1)
label variable r1guilt "r1guilt:w1 EURO-D: r Guilt (0,1)"
label values r1guilt heurod

*wave 1 spouse guilt
gen s1guilt =.
spouse r1guilt, result(s1guilt) wave(1)
label variable s1guilt "s1guilt:w1 EURO-D: s Guilt (0,1)"
label values s1guilt heurod

***EURO-D: Sleep***
*Wave 1 respondent sleep
gen r1sleep =.
missing_common euro5, result(r1sleep)
replace r1sleep = euro5 if inlist(euro5,0,1)
label variable r1sleep "r1sleep:w1 EURO-D: r Sleep (0,1)"
label values r1sleep heurod

*wave 1 spouse sleep
gen s1sleep =.
spouse r1sleep, result(s1sleep) wave(1)
label variable s1sleep "s1sleep:w1 EURO-D: s Sleep (0,1)"
label values s1sleep heurod

***EURO-D: Interest***
*Wave 1 respondent interest
gen r1intrst =.
missing_common euro6, result(r1intrst)
replace r1intrst = euro6 if inlist(euro6,0,1)
label variable r1intrst "r1intrst:w1 EURO-D: r Interest (0,1)"
label values r1intrst heurod

*wave 1 spouse interest
gen s1intrst =.
spouse r1intrst, result(s1intrst) wave(1)
label variable s1intrst "s1intrst:w1 EURO-D: s Interest (0,1)"
label values s1intrst heurod

***EURO-D: Irritability***
*Wave 1 respondent irritability
gen r1irritb =.
missing_common euro7, result(r1irritb)
replace r1irritb = euro7 if inlist(euro7,0,1)
label variable r1irritb "r1irritb:w1 EURO-D: r Irritability (0,1)"
label values r1irritb heurod

*wave 1 spouse irritability
gen s1irritb =.
spouse r1irritb, result(s1irritb) wave(1)
label variable s1irritb "s1irritb:w1 EURO-D: s Irritability (0,1)"
label values s1irritb heurod

***EURO-D: Appetite***
*Wave 1 respondent appetite
gen r1appett =.
missing_common euro8, result(r1appett)
replace r1appett = euro8 if inlist(euro8,0,1)
label variable r1appett "r1appett:w1 EURO-D: r Appetite (0,1)"
label values r1appett heurod

*wave 1 spouse appetite
gen s1appett =.
spouse r1appett, result(s1appett) wave(1)
label variable s1appett "s1appett:w1 EURO-D: s Appetite (0,1)"
label values s1appett heurod

***EURO-D: Fatigue***
*Wave 1 respondent fatigue
gen r1fatig =.
missing_common euro9, result(r1fatig)
replace r1fatig = euro9 if inlist(euro9,0,1)
label variable r1fatig "r1fatig:w1 EURO-D: r Fatigue (0,1)"
label values r1fatig heurod

*wave 1 spouse fatigue
gen s1fatig =.
spouse r1fatig, result(s1fatig) wave(1)
label variable s1fatig "s1fatig:w1 EURO-D: s Fatigue (0,1)"
label values s1fatig heurod

***EURO-D: Concentration***
*Wave 1 respondent concentration
gen r1concnt =.
missing_common euro10, result(r1concnt)
replace r1concnt = euro10 if inlist(euro10,0,1)
label variable r1concnt "r1concnt:w1 EURO-D: r Concentration (0,1)"
label values r1concnt heurod

*wave 1 spouse concentration
gen s1concnt =.
spouse r1concnt, result(s1concnt) wave(1)
label variable s1concnt "s1concnt:w1 EURO-D: s Concentration (0,1)"
label values s1concnt heurod

***EURO-D: Enjoyment***
*Wave 1 respondent enjoyment
gen r1enjoym =.
missing_common euro11, result(r1enjoym)
replace r1enjoym = euro11 if inlist(euro11,0,1)
label variable r1enjoym "r1enjoym:w1 EURO-D: r Enjoyment (0,1)"
label values r1enjoym heurod

*wave 1 spouse enjoyment
gen s1enjoym =.
spouse r1enjoym, result(s1enjoym) wave(1)
label variable s1enjoym "s1enjoym:w1 EURO-D: s Enjoyment (0,1)"
label values s1enjoym heurod

***EURO-D: Tearfulness***
*wave 1 respondent tearfulness
gen r1tearfl =.
missing_common euro12, result(r1tearfl)
replace r1tearfl = euro12 if inlist(euro12,0,1)
label variable r1tearfl "r1tearfl:w1 EURO-D: r Tearfulness (0,1)"
label values r1tearfl heurod

*wave 1 spouse tearfulness
gen s1tearfl =.
spouse r1tearfl, result(s1tearfl) wave(1)
label variable s1tearfl "s1tearfl:w1 EURO-D: s Tearfulness (0,1)"
label values s1tearfl heurod

***EURO-D: Score***
*wave 1 respondent EURO-D score
gen r1eurod =.
replace r1eurod = eurod
label variable r1eurod "r1eurod:w1 EURO-D: r Score"

*wave 1 respondent flag for EURO-D score
gen r1feurod = .
replace r1feurod = 1 if eurod_f==3
replace r1feurod = 5 if eurod_f==14
label variable r1feurod "r1feurod:w1 EURO-D Flag: r Score"
label values r1feurod healthimpflag

*wave 1 spouse EURO-D score
gen s1eurod =.
spouse r1eurod, result(s1eurod) wave(1)
label variable s1eurod "s1eurod:w1 EURO-D: s Score"

*wave 1 spouse flag for EURO-D score
gen s1feurod =.
spouse r1feurod, result(s1feurod) wave(1)
label variable s1feurod "s1feurod:w1 EURO-D Flag: s Score"
label values s1feurod healthimpflag

***Ever had high blood pressure***
*wave 1 respondent ever had high blood pressure
gen r1hibpe =.
missing_common ph006d2, result(r1hibpe)
replace r1hibpe = ph006d2 if inrange(ph006d2,0,1)
label variable r1hibpe "r1hibpe:w1 r ever had high blood pressure"
label values r1hibpe health_problems

*wave 1 spouse ever had high blood pressure
gen s1hibpe =.
spouse r1hibpe, result(s1hibpe) wave(1)
label variable s1hibpe "s1hibpe:w1 s ever had high blood pressure"
label values s1hibpe health_problems

***Ever had diabetes***
*wave 1 respondent ever had diabetes
gen r1diabe =.
missing_common ph006d5, result(r1diabe)
replace r1diabe = ph006d5 if inrange(ph006d5,0,1)
label variable r1diabe "r1diabe:w1 r ever had diabetes"

*wave 1 spouse ever had diabetes
gen s1diabe =.
spouse r1diabe, result(s1diabe) wave(1)
label variable s1diabe "s1diabe:w1 s ever had diabetes"

*value labels
label values r1diabe health_problems
label values s1diabe health_problems

***Ever had cancer***
*wave 1 respondent ever had cancer
gen r1cancre =.
missing_common ph006d10, result(r1cancre)
replace r1cancre = ph006d10 if inrange(ph006d10,0,1)
label variable r1cancre "r1cancre:w1 r ever had cancer"
label values r1cancre health_problems

*wave 1 respondent cancer dispute flag
gen r1cancrf = 0 if inlist(ph006d10,0,1)
label variable r1cancrf "r1cancrf:w1 r cancer dispute flag"
label values r1cancrf ehealth_problems_flag

***Ever had lung disease***
*wave 1 respondent ever had lung disease
gen r1lunge =.
missing_common ph006d6, result(r1lunge)
replace r1lunge = ph006d6 if inrange(ph006d6,0,1) 
label variable r1lunge "r1lunge:w1 r ever had lung disease"

*wave 1 spouse ever had lung disease
gen s1lunge =.
spouse r1lunge, result(s1lunge) wave(1)
label variable s1lunge "s1lunge:w1 s ever had lung disease"

*value labels
label values r1lunge health_problems
label values s1lunge health_problems

***Ever had heart problems***
*wave 1 respondent ever had heart problems
gen r1hearte =.
missing_common ph006d1, result(r1hearte)
replace r1hearte = 0 if inrange(ph006d1,0,1)
replace r1hearte = 1 if ph006d1 == 1
label variable r1hearte "r1hearte:w1 r ever had heart problems"
label values r1hearte health_problems

*wave 1 respondent heart problems dispute flag
gen r1heartf = 0 if inrange(ph006d1,0,1)
label variable r1heartf "r1heartf:w1 r heart problems dispute flag"
label values r1heartf ehealth_problems_flag

***Ever had stroke***
*wave 1 respondent ever had stroke
gen r1stroke =.
missing_common ph006d4 , result(r1stroke)
replace r1stroke = ph006d4 if inrange(ph006d4,0,1) 
label variable r1stroke "r1stroke:w1 r ever had stroke"
label values r1stroke health_problems

*wave 1 respondent stroke dispute flag
gen r1strokf = 0 if inrange(ph006d4,0,1)
label variable r1strokf "r1strokf:w1 r stroke dispute flag"
label values r1strokf ehealth_problems_flag

***Ever had arthritis***
*wave 1 respondent ever had arthritis
gen r1arthre =.
missing_common ph006d8 , result(r1arthre)
replace r1arthre = ph006d8 if inrange(ph006d8,0,1) 
label variable r1arthre "r1arthre:w1 r ever had arthritis"

*wave 1 spouse ever had arthritis
gen s1arthre =.
spouse r1arthre, result(s1arthre) wave(1)
label variable s1arthre "s1arthre:w1 s ever had arthritis"

*value labels
label values r1arthre health_problems
label values s1arthre health_problems

***Ever had asthma***
*wave 1 respondent ever had asthma
gen r1asthmae = .
missing_common ph006d7, result(r1asthmae)
replace r1asthmae = ph006d7 if inrange(ph006d7,0,1)
label variable r1asthmae "r1asthmae:w1 r ever had asthma"
label values r1asthmae health_problems

*wave 1 spouse ever had asthma
gen s1asthmae = .
spouse r1asthmae, result(s1asthmae) wave(1)
label variable s1asthmae "s1asthmae:w1 s ever had asthma"
label values s1asthmae health_problems

***Ever had high cholesterol***
*wave 1 respondent ever had high cholesterol
gen r1hchole = .
missing_common ph006d3, result(r1hchole)
replace r1hchole = ph006d3 if inrange(ph006d3,0,1)
label variable r1hchole "r1hchole:w1 r ever had high cholesterol"
label values r1hchole health_problems

*wave 1 spouse ever had high cholesterol
gen s1hchole = .
spouse r1hchole, result(s1hchole) wave(1)
label variable s1hchole "s1hchole:w1 s ever had high cholesterol"
label values s1hchole health_problems

***Ever had Parkinsons***
*wave 1 respondent ever had parkinsons
gen r1parkine = .
missing_common ph006d12, result(r1parkine)
replace r1parkine = ph006d12 if inrange(ph006d12,0,1)
label variable r1parkine "r1parkine:w1 r ever had parkinson disease"
label values r1parkine health_problems

*wave 1 spouse ever had parkinsons
gen s1parkine = .
spouse r1parkine, result(s1parkine) wave(1)
label variable s1parkine "s1parkine:w1 s ever had parkinson disease"
label values s1parkine health_problems

***Ever had cataracts***
*wave 1 respondent ever had cataracts
gen r1catrcte = .
missing_common ph006d13, result(r1catrcte)
replace r1catrcte = ph006d13 if inrange(ph006d13,0,1)
label variable r1catrcte "r1catrcte:w1 r ever had cataracts"
label values r1catrcte health_problems

*wave 1 spouse ever had cataracts
gen s1catrcte = .
spouse r1catrcte, result(s1catrcte) wave(1)
label variable s1catrcte "s1catrcte:w1 s ever had cataracts"
label values s1catrcte health_problems

***Ever had hip or femoral fracture***
*wave 1 respondent ever had hip or femoral fracture
gen r1hipfeme = .
missing_common ph006d14, result(r1hipfeme)
replace r1hipfeme = ph006d14 if inrange(ph006d14,0,1)
label variable r1hipfeme "r1hipfeme:w1 r ever had hip/femoral fracture"
label values r1hipfeme health_problems

*wave 1 respondent hip/femoral fracture dispute flag
gen r1hipfemf = 0
label variable r1hipfemf "r1hipfemf:w1 r hip/femoral fracture dispute flag"
label values r1hipfemf ehealth_problems_flag

***Ever had ulcer***
*wave 1 respondent ever had ulcer
gen r1ulcer =.
missing_common ph006d11 , result(r1ulcer)
replace r1ulcer = ph006d11 if inrange(ph006d11,0,1) 
label variable r1ulcer "r1ulcer:w1 r ever had ulcer"

*wave 1 spouse ever had ulcer
gen s1ulcer =.
spouse r1ulcer, result(s1ulcer) wave(1)
label variable s1ulcer "s1ulcer:w1 s ever had ulcer"

*value labels
label values r1ulcer health_problems
label values s1ulcer health_problems

***Height in meters***
*wave 1 respondent height in meters
gen r1height =.
missing_common ph013_, result(r1height)
replace r1height = .i if inrange(ph013_,0,89) 
replace r1height = ph013_ if inrange(ph013_,1.00,1.90) & inw1==1
replace r1height = ph013_/100 if inrange(ph013_,90,220) & inw1==1
label variable r1height "r1height:w1 r Height in meters"

*wave 1 spouse height in meters
gen s1height =.
spouse r1height, result(s1height) wave(1)
label variable s1height "s1height:w1 s Height in meters"

***Weight in kilograms***
*wave 1 respondent weight in kilograms
gen r1weight =.
missing_common ph012_, result(r1weight)
replace r1weight = .i if inrange(ph012_,0,12) 
replace r1weight = ph012_ if inrange(ph012_,13,250) 
label variable r1weight "r1weight:w1 r Weight in kilograms"
 
*wave 1 spouse weight in kilograms
gen s1weight =.
spouse r1weight, result(s1weight) wave(1)
label variable s1weight "s1weight:w1 s Weight in kilograms"

***BMI***
*wave 1 respondent BMI
gen r1bmi =.
missing_h r1weight r1height, result(r1bmi)
replace r1bmi = r1weight/(r1height^2) if !mi(r1weight) & !mi(r1height)
replace r1bmi = .i if (r1weight == .i | r1height == .i | !inrange(r1bmi,12,100)) & inw`wv' == 1
label variable r1bmi "r1bmi:w1 r Body Mass Index=kg/m2"

*wave 1 spouse BMI
gen s1bmi =.
spouse r1bmi, result(s1bmi) wave(1)
label variable s1bmi "s1bmi:w1 s Body Mass Index=kg/m2"

***BMI Categorization***
*wave 1 respondent BMI categorization
gen r1bmicat = .
missing_h r1weight r1height, result(r1bmicat)
replace r1bmicat = 1 if inrange(r1bmi,1,18.49999)
replace r1bmicat = 2 if inrange(r1bmi,18.50,24.99999)
replace r1bmicat = 3 if inrange(r1bmi,25,29.99999)
replace r1bmicat = 4 if inrange(r1bmi,30,34.99999)
replace r1bmicat = 5 if inrange(r1bmi,35,39.99999)
replace r1bmicat = 6 if inrange(r1bmi,40,100)
replace r1bmicat = .i if r1weight == .i | r1height == .i | r1bmi==.i
label variable r1bmicat "r1bmicat:w1 r BMI categorization"
label values r1bmicat bmicate

*wave 1 spouse BMI categorization
gen s1bmicat = .
spouse r1bmicat, result(s1bmicat) wave(1)
label variable s1bmicat "s1bmicat:w1 s BMI categorization"
label values s1bmicat bmicate

***Vigorous physical activity***
*wave 1 respondent vigorous physical activity
gen r1vgactx =.
missing_common br015_, result(r1vgactx)
replace r1vgactx = 2 if br015_ == 1
replace r1vgactx = 3 if br015_ == 2
replace r1vgactx = 4 if br015_ == 3
replace r1vgactx = 5 if br015_ == 4
label variable r1vgactx "r1vgactx:w1 R Freq vigorous phys activ"

*wave 1 spouse vigorous physical activity
gen s1vgactx =.
spouse r1vgactx, result(s1vgactx) wave(1)
label variable s1vgactx "s1vgactx:w1 S Freq vigorous phys activ"

*label values
label values r1vgactx activity
label values s1vgactx activity

***Moderate physical activity***
*wave 1 respondent moderate physical activity
gen r1mdactx =.
missing_common br016_, result(r1mdactx)
replace r1mdactx = 2 if br016_ == 1
replace r1mdactx = 3 if br016_ == 2
replace r1mdactx = 4 if br016_ == 3
replace r1mdactx = 5 if br016_ == 4
label variable r1mdactx "r1mdactx:w1 R Freq moderate phys activ"

*wave 1 spouse vigorous moderate activity
gen s1mdactx =.
spouse r1mdactx, result(s1mdactx) wave(1)
label variable s1mdactx "s1mdactx:w1 S Freq moderate phys activ"

*label values
label values r1mdactx activity
label values s1mdactx activity

***Ever drinks alcohol***
**last 7 days
*wave 1 respondent ever drinks alcohol
gen r1drink =.
missing_common br010_, result(r1drink)
replace r1drink = 0 if inlist(br010_,5,6,7)
replace r1drink = 1 if inlist(br010,1,2,3,4)
label variable r1drink "r1drink:w1 R ever drinks any alcohol"

*wave 1 spouse ever drinks alcohol
gen s1drink =.
spouse r1drink, result(s1drink) wave(1)
label variable s1drink "s1drink:w1 S ever drinks any alcohol"

*label values
label values r1drink ever_drink
label values s1drink ever_drink

***Days per week drinks***
*wave 1 respondent days per week drinks
gen r1drinkx =.
missing_common br010_ , result(r1drinkx)
replace r1drinkx = 0 if inlist(br010_,6,7)
replace r1drinkx = 1 if br010_ == 5
replace r1drinkx = 2 if inlist(br010_,4,3) 
replace r1drinkx = 3 if br010_ == 2 
replace r1drinkx = 4 if br010_ == 1 
label variable r1drinkx "r1drinkx:w1 R Frequency of drinking"
label values r1drinkx drinkx

*wave 1 spouse days per week drinks
gen s1drinkx =.
spouse r1drinkx, result(s1drinkx) wave(1)
label variable s1drinkx "s1drinkx:w1 S Frequency of drinking"
label values s1drinkx drinkx

***Ever smoked cigarettes/pipe/cigar***
*wave 1 respondent ever smoked cigarettes/pipe/cigar
gen r1smokev =.
missing_common br001_, result(r1smokev)
replace r1smokev = 0 if br001_ == 5
replace r1smokev = 1 if br001_ == 1
label variable r1smokev "r1smokev:w1 R smoke ever"

*wave 1 spouse ever smoked cigarettes/pipe/cigar
gen s1smokev =.
spouse r1smokev, result(s1smokev) wave(1)
label variable s1smokev "s1smokev:w1 S smoke ever"

*label values
label values r1smokev smokes
label values s1smokev smokes

***Smokes cigarettes/pipe/cigar now***
*wave 1 respondent smokes cigarettes/pipe/cigar now
gen r1smoken =.
missing_common br001_ br002_, result(r1smoken)
replace r1smoken = 0 if br001_ == 5 | br002_ == 5
replace r1smoken = 1 if br002_ == 1
label variable r1smoken "r1smoken:w1 R smokes now"

*wave 1 spouse smokes cigarettes/pipe/cigar now
gen s1smoken =.
spouse r1smoken, result(s1smoken) wave(1)
label variable s1smoken "s1smoken:w1 S smokes now"

*label values
label values r1smoken smokes
label values s1smoken smokes

***Number cigarettes/pipe/cigar per day when smokes***
*wave 1 respondent number cigarettes/pipe/cigar per day when smokes
gen br006n = br006_ if inrange(br006_,0,120)
gen br007n = br007_ if inrange(br007_,0,120)
gen br008n = br008_ if inrange(br008_,0,120)
egen r1smokef = rowtotal (br006n br007n br008n), m
replace r1smokef = .m if (br006_ == . & br007_ == . & br008_ == .) & inw`wv' == 1
replace r1smokef = .d if r1smokef == . & (br006_ == -1 | br007_ == -1 | br008_ == -1)
replace r1smokef = .r if r1smokef == . & (br006_ == -2 | br007_ == -2 | br008_ == -2)
replace r1smokef = 0 if br001_ == 5 | (br001_ == 1 & br002_ == 5)
label variable r1smokef "r1smokef:w1 r amount smokes per day"

*wave 1 spouse number cigarettes/pipe/cigar per day when smokes
gen s1smokef = .
spouse r1smokef, result(s1smokef) wave(1)
label variable s1smokef "s1smokef:w1 s amount smokes per day"

drop br006n br007n br008n


***drop wave 1 Behavioural Risk file raw variables
drop `health_w1_br' 

***drop wave 1 Mental Health file raw variables
drop `health_w1_mh'

***drop wave 1 Physical Health file raw variables
drop `health_w1_ph'

***drop wave 1 Generated Variables (Health) file raw variables
drop `health_w1_gv_health'

***drop wave 1 Dropoff file raw variables
drop `health_w1_dropoff'

***drop wave 1 Vignette file raw variables
drop `health_w1_vignettes'

***drop wave 1 imputation file raw variables***
drop `health_w1_imput'

******************************************************************************************



*set wave number
local wv = 2
local pre_wv = `wv' - 1

***merge with wave 2 Behavioural Risk data***
local health_w2_br br015_ br016_ br010_ br001_ br002_ br022_ br006_ br007_ br008_ br019_ 
merge 1:1 mergeid using "$wave_2_br", keepusing(`health_w2_br') nogen

***merge with wave 2 Mental Health data***
local health_w2_mh mh020_ mh018_ mh022
merge 1:1 mergeid using "$wave_2_mh", keepusing(`health_w2_mh') nogen

***merge with wave 2 Physical Health data***
local health_w2_ph ph003_ ph005_ ph049d2 ph049d1 ph049d3 ///
					ph049d4 ph049d5 ph049d6 ph049d7 ph049d10 ph049d13 ph049d11 ph049d12 ///
					ph049d9 ph049d8 ph048d1 ph048d2 ph048d3 ph048d4 ph048d5 ph048d6 ///
					ph048d9 ph048d10 ph048d7 ph048d8 ph006d2 ph006d3 ph006d5 ph006d10 ///
					ph006d6 ph006d1 ph006d4 ph006d7 ph006d8 ph006d12 ph006d13 ph006d14 ///
					ph013_ ph012_ ph006d11 ph006d16 ph062_ ph063_ ph064_ ph067_? ph068_?
merge 1:1 mergeid using "$wave_2_ph", keepusing(`health_w2_ph') nogen

***merge with wave 2 Activities data***
local health_w2_ac ac027_ ac028_ ac029_ ac030_ ac031_ ac033_ ac034_ ac032_ ac199_ 
merge 1:1 mergeid using "$wave_2_ac", keepusing(`health_w2_ac') nogen

***merge with wave 2 Generated Variables (Health) data***
local health_w2_gv_health euro1 euro2 euro3 euro4 euro5 euro6 euro7 ///
                          euro8 euro9 euro10 euro11 euro12 
merge 1:1 mergeid using "$wave_2_gv_health", keepusing(`health_w2_gv_health') nogen

***merge with wave 2 imputation data***
local health_w2_imput country eurod eurod_f
merge 1:1 mergeid using "$wave_2_imputations_", keepusing(`health_w2_imput') nogen



***self-report of health***
*wave 2 respondnet self-report of health
gen r2shlt =.
missing_common ph003_ , result(r2shlt)
replace r2shlt = 1 if ph003_==1
replace r2shlt = 2 if ph003_==2
replace r2shlt = 3 if ph003_==3
replace r2shlt = 4 if ph003_==4 
replace r2shlt = 5 if ph003_==5 
label variable r2shlt "r2shlt:w2 r self-report of health"

*wave 2 spouse self-report of health
gen s2shlt =.
spouse r2shlt, result(s2shlt) wave(2)
label variable s2shlt "s2shlt:w2 s self-report of health"

*value labels
label values r2shlt health
label values s2shlt health

***self-report change of health***
*wave 2 respondent self-report change of health
gen r2shltc_s = .
missing_common ph062_ ph063_ ph064_, result(r2shltc_s)
replace r2shltc_s = .c if inw2==1 & inw1==0
replace r2shltc_s = 1 if ph062_ == 1 & ph063_ == 2
replace r2shltc_s = 2 if ph062_ == 1 & ph063_ == 1
replace r2shltc_s = 3 if ph062_ == 2
replace r2shltc_s = 4 if ph062_ == 3 & ph064_ == 1
replace r2shltc_s = 5 if ph062_ == 3 & ph064_ == 2
label variable r2shltc_s "r2shltc_s:w2 r self-reported change of health"
label values r2shltc_s hlthchange

*wave 2 spouse self-report change on health
gen s2shltc_s = .
spouse r2shltc_s, result(s2shltc_s) wave(2)
label variable s2shltc_s "s2shltc_s:w2 s self-reported change of health"
label values s2shltc_s hlthchange

***Whether health limits activities***
*wave 2 respodnet whether health limits activities
gen r2hlthlma =.
missing_common ph005_, result(r2hlthlma)
replace r2hlthlma = 0 if ph005_ == 3
replace r2hlthlma = 1 if inrange(ph005_,1,2)
label variable r2hlthlma "r2hlthlma:w2 r hlth problem limits activities"

*wave 2 spouse whether healh limits activities
gen s2hlthlma =.
spouse r2hlthlma, result(s2hlthlma) wave(2)
label variable s2hlthlma "s2hlthlma:w2 s hlth problem limits activities"

*value labels
label values r2hlthlma limits
label values s2hlthlma limits

***Some difficulty walking cross room***
*wave 2 respondent difficulty walking across room
gen r2walkra =.
missing_common ph049d2, result(r2walkra)	
replace r2walkra = 0 if ph049d2==0 
replace r2walkra = 1 if ph049d2==1 
label variable r2walkra "r2walkra:w2 R Some Diff-Walk across room"

*wave 2 spouse difficulty walking across room
gen s2walkra =.
spouse r2walkra, result(s2walkra) wave(2)
label variable s2walkra "s2walkra:w2 S Some Diff-Walk across room"

*value labels
label values r2walkra adldiff
label values s2walkra adldiff

***Some difficulty dressing***
*wave 2 respondent difficulty dressing
gen r2dressa =.
missing_common ph049d1 , result(r2dressa)		
replace r2dressa = 0 if ph049d1==0 
replace r2dressa = 1 if ph049d1==1 
label variable r2dressa "r2dressa:w2 R Some Diff-Dressing"

*wave 2 spouse difficulty dressing
gen s2dressa =.
spouse r2dressa, result(s2dressa) wave(2)
label variable s2dressa "s2dressa:w2 S Some Diff-Dressing"

*value labels
label values r2dressa adldiff
label values s2dressa adldiff

***Some difficulty bathing, shower***
*wave 2 respondent difficulty bathing, shower
gen r2batha =.
missing_common ph049d3 , result(r2batha)		
replace r2batha = 0 if ph049d3==0 
replace r2batha = 1 if ph049d3==1 
label variable r2batha "r2batha:w2 R Some Diff-Bathing, shower"

*wave 2 spouse difficulty bathing, shower
gen s2batha =.
spouse r2batha, result(s2batha) wave(2)
label variable s2batha "s2batha:w2 S Some Diff-Bathing, shower"

*value labels
label values r2batha adldiff
label values s2batha adldiff

***Some difficulty eating***
*wave 2 respondent difficulty eating
gen r2eata =.
missing_common ph049d4 , result(r2eata)
replace r2eata = 0 if ph049d4==0 
replace r2eata = 1 if ph049d4==1 
label variable r2eata "r2eata:w2 R Some Diff-Eating"

*wave 2 spouse difficulty eating
gen s2eata =.
spouse r2eata, result(s2eata) wave(2)
label variable s2eata "s2eata:w2 S Some Diff-Eating"

*value labels
label values r2eata adldiff
label values s2eata adldiff

***Some difficulty getting in/out of bed***
*wave 2 respondent difficulty getting in/out of bed
gen r2beda =.
missing_common ph049d5 , result(r2beda)
replace r2beda = 0 if ph049d5==0  
replace r2beda = 1 if ph049d5==1 
label variable r2beda "r2beda:w2 R Some Diff-Get in/out bed"

*wave 2 spouse difficulty getting in/out of bed
gen s2beda =.
spouse r2beda, result(s2beda) wave(2)
label variable s2beda "s2beda:w2 S Some Diff-Get in/out bed"

*value labels
label values r2beda adldiff
label values s2beda adldiff

***Some difficulty using the toilet***
*wave 2 respondent difficulty using the toilet
gen r2toilta =.
missing_common ph049d6 , result(r2toilta)			
replace r2toilta = 0 if ph049d6==0  
replace r2toilta = 1 if ph049d6==1 
label variable r2toilta "r2toilta:w2 R Some Diff-Using the toilet"

*wave 2 spouse difficulty using the toilet
gen s2toilta =.
spouse r2toilta, result(s2toilta) wave(2)
label variable s2toilta "s2toilta:w2 S Some Diff-Using the toilet"

*value labels
label values r2toilta adldiff
label values s2toilta adldiff

***Some difficulty using using a map***
*wave 2 respondent difficulty using a map
gen r2mapa =.
missing_common ph049d7 , result(r2mapa)	
replace r2mapa = 0 if ph049d7==0 
replace r2mapa = 1 if ph049d7==1  
label variable r2mapa "r2mapa:w2 R Some Diff-Use a map"

*wave 2 spouse difficulty using a map
gen s2mapa =.
spouse r2mapa, result(s2mapa) wave(2)
label variable s2mapa "s2mapa:w2 S Some Diff-Use a map"

*value labels
label values r2mapa adldiff
label values s2mapa adldiff

***Some difficulty using a telephone***
*wave 2 respondent difficulty using a telephone
gen r2phonea =.
missing_common ph049d10, result(r2phonea)			
replace r2phonea = 0 if ph049d10==0 
replace r2phonea = 1 if ph049d10==1 
label variable r2phonea "r2phonea:w2 R Some Diff-Use telephone"

*wave 2 spouse difficulty using a telephone
gen s2phonea =.
spouse r2phonea, result(s2phonea) wave(2)
label variable s2phonea "s2phonea:w2 S Some Diff-Use a telephone"

*value labels
label values r2phonea adldiff
label values s2phonea adldiff

***Some difficulty manging money***
*wave 2 respondent difficulty manging money
gen r2moneya =.
missing_common ph049d13 , result(r2moneya)				
replace r2moneya = 0 if ph049d13==0 
replace r2moneya = 1 if ph049d13==1
label variable r2moneya "r2moneya:w2 R Some Diff-Managing money"

*wave 2 spouse difficulty manging money
gen s2moneya =.
spouse r2moneya, result(s2moneya) wave(2)
label variable s2moneya "s2moneya:w2 S Some Diff-Managing money"

*value labels
label values r2moneya adldiff
label values s2moneya adldiff

***Some difficulty taking medications***
*wave 2 respondent difficulty taking medications
gen r2medsa =.
missing_common ph049d11 , result(r2medsa)					
replace r2medsa = 0 if ph049d11==0 
replace r2medsa = 1 if ph049d11==1 
label variable r2medsa "r2medsa:w2 R Some Diff-Take medications"

*wave 2 spouse difficulty taking medications
gen s2medsa =.
spouse r2medsa, result(s2medsa) wave(2)
label variable s2medsa "s2medsa:w2 S Some Diff-Take medications"

*value labels
label values r2medsa adldiff
label values s2medsa adldiff

***Some difficulty shopping for groceries***
*wave 2 respondent difficulty shopping for groceries
gen r2shopa =.
missing_common ph049d9, result(r2shopa)			
replace r2shopa = 0 if ph049d9==0 
replace r2shopa = 1 if ph049d9==1 
label variable r2shopa "r2shopa:w2 R Some Diff-Shop for grocery"

*wave 1 spouse difficulty shopping for groceries
gen s2shopa =.
spouse r2shopa, result(s2shopa) wave(1)
label variable s2shopa "s2shopa:w2 S Some Diff-Shop for grocery"

*value labels
label values r2shopa adldiff
label values s2shopa adldiff

***Some difficulty preparing a hot meal***
*wave 2 respondent difficulty preparing a hot meal
gen r2mealsa =.
missing_common ph049d8 , result(r2mealsa)
replace r2mealsa = 0 if ph049d8==0  
replace r2mealsa = 1 if ph049d8==1 
label variable r2mealsa "r2mealsa:w2 R Some Diff-Prepare hot meal"

*wave 2 spouse difficulty preparing a hot meal
gen s2mealsa =.
spouse r2mealsa, result(s2mealsa) wave(2)
label variable s2mealsa "s2mealsa:w2 S Some Diff-Prepare hot meal"

*value labels
label values r2mealsa adldiff
label values s2mealsa adldiff

***Some difficulty doing work around the house***
*wave 2 respondent difficulty doing work around the house
gen r2housewka = .
missing_common ph049d12, result(r2housewka)
replace r2housewka = 0 if ph049d12==0
replace r2housewka = 1 if ph049d12==1
label variable r2housewka "r2housewka:w2 R some diff-doing work around house"
label values r2housewka adldiff

*wave 2 spouse difficulty doing work around the house
gen s2housewka = .
spouse r2housewka, result(s2housewka) wave(2)
label variable s2housewka "s2housewka:w2 s some diff-doing work around house"
label values s2housewka adldiff

***Difficulty Walking 100m***
*Wave 2 respondent difficulty walking 100m
gen r2walk100a =.
missing_common ph048d1 , result(r2walk100a)
replace r2walk100a = 0 if ph048d1==0  
replace r2walk100a = 1 if ph048d1==1 
label variable r2walk100a "r2walk100a:w2 R Some Diff-Walk 100m"

*wave 2 spouse difficulty walking 100m
gen s2walk100a =.
spouse r2walk100a, result(s2walk100a) wave(2)
label variable s2walk100a "s2walk100a:w2 S Some Diff-Walk 100m"

*value labels
label values r2walk100a adldiff
label values s2walk100a adldiff

***Difficulty sitting for 2 hours***
*Wave 2 respondent difficulty sitting for 2 hours
gen r2sita =.
missing_common ph048d2 , result(r2sita)
replace r2sita = 0 if ph048d2==0
replace r2sita = 1 if ph048d2==1 
label variable r2sita "r2sita:w2 R Some Diff-Sit for 2 hours"

*wave 1 spouse difficulty sitting for 2 hours
gen s2sita =.
spouse r2sita, result(s2sita) wave(2)
label variable s2sita "s2sita:w2 S Some Diff-Sit for 2 hours"

*value labels
label values r2sita adldiff
label values s2sita adldiff

***Difficulty getting up from a chair***
*Wave 2 respondent difficulty getting up from a chair
gen r2chaira =.
missing_common ph048d3  , result(r2chaira)	
replace r2chaira = 0 if ph048d3==0 
replace r2chaira = 1 if ph048d3==1
label variable r2chaira "r2chaira:w2 R Some Diff-Get up fr chair"

*wave 2 spouse difficulty getting up from a chair
gen s2chaira =.
spouse r2chaira, result(s2chaira) wave(2)
label variable s2chaira "s2chaira:w2 S Some Diff-Get up fr chair"

*value labels
label values r2chaira adldiff
label values s2chaira adldiff

***Difficulty climbing several flights of stairs***
*Wave 2 respondent difficulty climbing several flights of stairs
gen r2climsa =.
missing_common ph048d4 , result(r2climsa)
replace r2climsa = 0 if ph048d4==0 
replace r2climsa = 1 if ph048d4==1 
label variable r2climsa "r2climsa:w2 R Some Diff-Clmb sev flt str"

*wave 2 spouse difficulty climbing several flights of stairs
gen s2climsa =.
spouse r2climsa, result(s2climsa) wave(2)
label variable s2climsa "s2climsa:w2 S Some Diff-Clmb sev flt str"

*value labels
label values r2climsa adldiff
label values s2climsa adldiff

***Difficulty climbing one flight of stairs***
*Wave 2 respondent difficulty climbing one flight of stairs
gen r2clim1a =.
missing_common ph048d5 , result(r2clim1a)		
replace r2clim1a = 0 if ph048d5==0 
replace r2clim1a = 1 if ph048d5==1
label variable r2clim1a "r2clim1a:w2 R Some Diff-Clmb 1 flt str"

*wave 2 spouse difficulty climbing one flight of stairs
gen s2clim1a =.
spouse r2clim1a, result(s2clim1a) wave(2)
label variable s2clim1a "s2clim1a:w2 S Some Diff-Clmb 1 flt str"

*value labels
label values r2clim1a adldiff
label values s2clim1a adldiff

***Difficulty stooping/keeling/crouching***
*Wave 2 respondent difficulty stooping/keeling/crouching
gen r2stoopa =.
missing_common ph048d6, result(r2stoopa)		
replace r2stoopa = 0 if ph048d6==0
replace r2stoopa = 1 if ph048d6==1 
label variable r2stoopa "r2stoopa:w2 R Some Diff-Stoop/Kneel/Crch"

*wave 2 spouse difficulty stooping/keeling/crouching
gen s2stoopa =.
spouse r2stoopa, result(s2stoopa) wave(2)
label variable s2stoopa "s2stoopa:w2 S Some Diff-Stoop/Kneel/Crch"

*value labels
label values r2stoopa adldiff
label values s2stoopa adldiff

***Difficulty lifting/carrying 10lbs***
*Wave 2 respondent difficulty lifting/carrying 10lbs
gen r2lifta =.
missing_common ph048d9 , result(r2lifta)	
replace r2lifta = 0 if ph048d9==0
replace r2lifta = 1 if ph048d9==1
label variable r2lifta "r2lifta:w2 R Some Diff-Lift/carry 10lbs"

*wave 2 spouse difficulty lifting/carrying 10lbs
gen s2lifta =.
spouse r2lifta, result(s2lifta) wave(2)
label variable s2lifta "s2lifta:w2 S Some Diff-Lift/carry 10lbs"

*value labels
label values r2lifta adldiff
label values s2lifta adldiff

***Difficulty picking up a small coin***
*Wave 2 respondent difficulty picking up a small coin
gen r2dimea =.
missing_common ph048d10 , result(r2dimea)
replace r2dimea = 0 if ph048d10==0 
replace r2dimea = 1 if ph048d10==1 
label variable r2dimea "r2dimea:w2 R Some Diff-Pick up a small coin"

*wave 2 spouse difficulty picking up a small coin
gen s2dimea =.
spouse r2dimea, result(s2dimea) wave(2)
label variable s2dimea "s2dimea:w2 S Some Diff-Pick up a small coin"

*value labels
label values r2dimea adldiff
label values s2dimea adldiff

***Difficulty reaching/extending arms up***
*Wave 2 respondent difficulty reaching/extending arms up
gen r2armsa =.
missing_common ph048d7, result(r2armsa)
replace r2armsa = 0 if ph048d7==0
replace r2armsa = 1 if ph048d7==1
label variable r2armsa "r2armsa:w2 R Some Diff-Rch/xtnd arms up"

*wave 2 spouse difficulty reaching/extending arms up
gen s2armsa =.
spouse r2armsa, result(s2armsa) wave(2)
label variable s2armsa "s2armsa:w2 S Some Diff-Rch/xtnd arms up"

*value labels
label values r2armsa adldiff
label values s2armsa adldiff

***Difficulty pushing/pulling a large object***
*Wave 2 respondent difficulty pushing/pulling a large object
gen r2pusha =.
missing_common ph048d8, result(r2pusha)
replace r2pusha = 0 if ph048d8==0
replace r2pusha = 1 if ph048d8==1
label variable r2pusha "r2pusha:w2 R Some Diff-Push/pull lg obj"

*wave 2 spouse difficulty pushing/pulling a large object
gen s2pusha =.
spouse r2pusha, result(s2pusha) wave(2)
label variable s2pusha "s2pusha:w2 S Some Diff-Push/pull lg obj"

*value labels
label values r2pusha adldiff
label values s2pusha adldiff

***ADL Summary 0-5***
*Wave 2 respondent ADL number of missings
egen r2adlam = rowmiss(r2batha r2dressa r2eata r2beda r2walkra) if inw2 == 1
label variable r2adlam "r2adlam:w2 Missings in ADL summary"

*wave 2 spouse ADL number of missings
gen s2adlam =.
spouse r2adlam, result(s2adlam) wave(2)
label variable s2adlam "s2adlam:w2 Missings in ADL summary"

local adl
foreach v of varlist r2batha r2dressa r2eata r2beda r2walkra {
	recode `v' (miss = 0), generate(`v'_m)
	local adl `adl' `v'_m
}

*Wave 2 respondent ADL Summary 0-5
gen r2adla =.
missing_h r2batha r2dressa r2eata r2beda r2walkra, result(r2adla)
replace r2adla = r2batha_m + r2dressa_m + r2eata_m + r2beda_m + r2walkra_m if inrange(r2adlam,0,4)
label variable r2adla "r2adla:w2 R Some Diff-ADLs /0-5"

*wave 2 spouse ADL Summary 0-5
gen s2adla =.
spouse r2adla, result(s2adla) wave(2)
label variable s2adla "s2adla:w2 S Some Diff-ADLs /0-5"

*drop intermediate variables
drop `adl'

***ADL Wallace Summary 0-3***
*Wave 2 respondent ADL Wallace number of missings
egen r2adlwam = rowmiss(r2batha r2dressa r2eata) if inw2 == 1
label variable r2adlwam "r2adlwam:w2 r Missings in ADL Wallace summary"

*wave 2 spouse ADL Wallace number of missings
gen s2adlwam =.
spouse r2adlwam, result(s2adlwam) wave(2)
label variable s2adlwam "s2adlwam:w2 s Missings in ADL Wallace summary"

local adlw
foreach v of varlist r2batha r2dressa r2eata {
	recode `v' (miss = 0), generate(`v'_m)
	local adlw `adlw' `v'_m
}

*Wave 2 respondent ADL Wallace Summary 0-3
gen r2adlwa =.
missing_h r2batha r2dressa r2eata, result(r2adlwa)
replace r2adlwa = r2batha_m + r2dressa_m + r2eata_m if inrange(r2adlwam,0,2)
label variable r2adlwa "r2adlwa:w2 R Some Diff-ADLs:Wallace /0-3"

*wave 2 spouse ADL Wallace Summary 0-3
gen s2adlwa =.
spouse r2adlwa, result(s2adlwa) wave(2)
label variable s2adlwa "s2adlwa:w2 S Some Diff-ADLs:Wallace /0-3"

*drop intermediate variables
drop `adlw'

***IADL Summary 0-3***
*Wave 2 respondent IADL number of missings
egen r2iadlam = rowmiss(r2phonea r2moneya r2medsa) if inw2 == 1
label variable r2iadlam "r2iadlam:w2 r Missings in IADL 0-3 summary"

*wave 2 spouse IADL number of missings
gen s2iadlam =.
spouse r2iadlam, result(s2iadlam) wave(2)
label variable s2iadlam "s2iadlam:w2 s Missings in IADL 0-3 summary"

local iadl
foreach v of varlist r2phonea r2moneya r2medsa {
	recode `v' (miss = 0), generate(`v'_m)
	local iadl `iadl' `v'_m
} 

*Wave 2 respondent IADLs Summary 0-3
gen r2iadla =.
missing_h r2phonea r2moneya r2medsa, result(r2iadla)
replace r2iadla = r2phonea_m + r2moneya_m + r2medsa_m if inrange(r2iadlam,0,2)
label variable r2iadla "r2iadla:w2 R Some Diff-IADLs: /0-3"

*wave 2 spouse IADLs Summary 0-3
gen s2iadla =.
spouse r2iadla, result(s2iadla) wave(2)
label variable s2iadla "s2iadla:w2 S Some Diff-IADLs: /0-3"

*drop intermediate variables
drop `iadl'

***IADL Summary 0-5***
*Wave 2 respondent IADL number of missings
egen r2iadlzam = rowmiss(r2phonea r2moneya r2medsa r2shopa r2mealsa) if inw2 == 1
label variable r2iadlzam "r2iadlzam:w2 r Missings in IADL 0-5 summary"

*wave 2 spouse IADL number of missings
gen s2iadlzam =.
spouse r2iadlzam, result(s2iadlzam) wave(2)
label variable s2iadlzam "s2iadlzam:w2 s Missings in IADL 0-5 summary"

local iadlz
foreach v of varlist r2phonea r2moneya r2medsa r2shopa r2mealsa {
	recode `v' (miss = 0), generate(`v'_m)
	local iadlz `iadlz' `v'_m
} 

*Wave 2 respondent IADLs Summary 0-5
gen r2iadlza =.
missing_h r2phonea r2moneya r2medsa r2shopa r2mealsa, result(r2iadlza)
replace r2iadlza = r2phonea_m + r2moneya_m + r2medsa_m + r2shopa_m + r2mealsa_m if inrange(r2iadlzam,0,4)
label variable r2iadlza "r2iadlza:w2 R Some Diff-IADLs: /0-5"

*wave 2 spouse IADLs Summary 0-5
gen s2iadlza =.
spouse r2iadlza, result(s2iadlza) wave(2)
label variable s2iadlza "s2iadlza:w2 S Some Diff-IADLs: /0-5"

*drop intermediate variables
drop `iadlz'

***Mobility Summary***
*Wave 2 respondent Mobility Summary number of missings
egen r2mobilbm = rowmiss(r2walk100a r2walkra r2climsa r2clim1a) if inw2 == 1
label variable r2mobilbm "r2mobilbm:w2 r Missings in mobility summary"

*wave 2 spouse Mobility Summary number of missings
gen s2mobilbm =.
spouse r2mobilbm, result(s2mobilbm) wave(2)
label variable s2mobilbm "s2mobilbm:w2 s Missings in mobility summary"

local mobilb
foreach v of varlist r2walk100a r2walkra r2climsa r2clim1a {
	recode `v' (miss = 0), generate(`v'_m)
	local mobilb `mobilb' `v'_m
} 

*Wave 2 respondent Mobility Summary
gen r2mobilb =.
missing_h r2walk100a r2walkra r2climsa r2clim1a, result(r2mobilb)
replace r2mobilb = r2walk100a_m + r2walkra_m + r2climsa_m + r2clim1a_m if inrange(r2mobilbm,0,3)
label variable r2mobilb "r2mobilb:w2 R Some Diff-Mobility /0-4"

*wave 2 spouse Mobility Summary
gen s2mobilb =.
spouse r2mobilb, result(s2mobilb) wave(2)
label variable s2mobilb "s2mobilb:w2 S Some Diff-Mobility /0-4"

*drop intermediate variables
drop `mobilb'

***Large Muscle Summary***
*Wave 2 respondent Large Muscle Summary number of missings
egen r2lgmusam = rowmiss(r2sita r2chaira r2stoopa r2pusha) if inw2 == 1
label variable r2lgmusam "r2lgmusam:w2 r Missings in large muscle summary"

*wave 2 spouse Large Muscle Summary number of missings
gen s2lgmusam =.
spouse r2lgmusam, result(s2lgmusam) wave(2)
label variable s2lgmusam "s2lgmusam:w2 s Missings in large muscle summary"

local lgmusa
foreach v of varlist r2sita r2chaira r2stoopa r2pusha {
	recode `v' (miss = 0), generate(`v'_m)
	local lgmusa `lgmusa' `v'_m
} 

*Wave 2 respondent Large Muscle Summary
gen r2lgmusa =.
missing_h r2sita r2chaira r2stoopa r2pusha, result(r2lgmusa)
replace r2lgmusa = r2sita_m + r2chaira_m + r2stoopa_m + r2pusha_m if inrange(r2lgmusam,0,3)
label variable r2lgmusa "r2lgmusa:w2 R Some Diff-Large muscle /0-4"

*wave 2 spouse Large Muscle Summary
gen s2lgmusa =.
spouse r2lgmusa, result(s2lgmusa) wave(2)
label variable s2lgmusa "s2lgmusa:w2 S Some Diff-Large muscle /0-4"

*drop intermediate variables
drop `lgmusa'

***Gross Motor Summary***
*Wave 2 respondent Gross Motor Summary number of missings
egen r2grossam = rowmiss(r2walk100a r2walkra r2clim1a r2beda r2batha) if inw2 == 1
label variable r2grossam "r2grossam:w2 r Missings in wk,rn,clmb,bd,bth summary"

*wave 2 spouse Gross Motor Summary number of missings
gen s2grossam =.
spouse r2grossam, result(s2grossam) wave(2)
label variable s2grossam "s2grossam:w2 s Missings in wk,rn,clmb,bd,bth summary"

local grossa
foreach v of varlist r2walk100a r2walkra r2clim1a r2beda r2batha {
	recode `v' (miss = 0), generate(`v'_m)
	local grossa `grossa' `v'_m
}

*Wave 2 respondent Gross Motor Summary
gen r2grossa =.
missing_h r2walk100a r2walkra r2clim1a r2beda r2batha, result(r2grossa)
replace r2grossa = r2walk100a_m + r2walkra_m + r2clim1a_m + r2beda_m + r2batha_m if inrange(r2grossam,0,4)
label variable r2grossa "r2grossa:w2 R Some Diff-Wk,rn,clmb,bd,bth /0-5"

*wave 2 spouse Gross Motor Summary
gen s2grossa =.
spouse r2grossa, result(s2grossa) wave(2)
label variable s2grossa "s2grossa:w2 S Some Diff-Wk,rn,clmb,bd,bth /0-5"

*drop intermediate variables
drop `grossa'

***Fine Motor Summary***
*Wave 2 respondent Fine Motor Summary number of missings
egen r2fineam = rowmiss(r2dimea r2eata r2dressa) if inw2 == 1
label variable r2fineam "r2fineam:w2 r Missings in dime,eat,dress summary"

*wave 2 spouse Gross Fine Summary number of missings
gen s2fineam =.
spouse r2fineam, result(s2fineam) wave(2)
label variable s2fineam "s2fineam:w2 s Missings in dime,eat,dress summary"

local finea
foreach v of varlist r2dimea r2eata r2dressa {
	recode `v' (miss = 0), generate(`v'_m)
	local finea `finea' `v'_m
}

*Wave 2 respondent Fine Motor Summary
gen r2finea =.
missing_h r2dimea r2eata r2dressa, result(r2finea)
replace r2finea = r2dimea_m + r2eata_m + r2dressa_m if inrange(r2fineam,0,2)
label variable r2finea "r2finea:w2 R Some Diff-dime,eat,dress /0-3"

*wave 2 spouse Fine Motor Summary
gen s2finea =.
spouse r2finea, result(s2finea) wave(2)
label variable s2finea "s2finea:w2 S Some Diff-dime,eat,dress /0-3"

*drop intermediate variables
drop `finea'

***CESD: Felt Depressed*****
*Wave 2 respondent Felt Depressed
gen r2depres =.
missing_common ac027_ , result(r2depres)
replace r2depres = .s if ac199_ == 2
replace r2depres = 0 if ac027_==5
replace r2depres = 1 if ac027_==1
label variable r2depres "r2depres:w2 CESD: r Felt depressed (0,1)"

*wave 2 spouse Felt Depressed
gen s2depres =.
spouse r2depres, result(s2depres) wave(2)
label variable s2depres "s2depres:w2 CESD: s Felt depressed (0,1)"

*value labels
label values r2depres cesd
label values s2depres cesd

***CESD: Everything is an effort***
*Wave 2 respondent Everything is an effort
gen r2effort =.
missing_common ac028_ , result(r2effort)
replace r2effort = .s if ac199_ == 2
replace r2effort = 0 if ac028_==5
replace r2effort = 1 if ac028_==1
label variable r2effort "r2effort:w2 CESD: r Everything an effort (0,1)"

*wave 2 spouse Everything is an effort
gen s2effort =.
spouse r2effort, result(s2effort) wave(2)
label variable s2effort "s2effort:w2 CESD: s Everything an effort (0,1)"

*value labels
label values r2effort cesd
label values s2effort cesd

***CESD: Sleep was restless***
*Wave 2 respondent Sleep was restless
gen r2sleepr =.
missing_common ac029_ , result(r2sleepr)
replace r2sleepr = .s if ac199_ == 2
replace r2sleepr = 0 if ac029_==5
replace r2sleepr = 1 if ac029_==1
label variable r2sleepr "r2sleepr:w2 CESD: r Sleep was restless (0,1)"

*wave 2 spouse Sleep was restless
gen s2sleepr =.
spouse r2sleepr, result(s2sleepr) wave(2)
label variable s2sleepr "s2sleepr:w2 CESD: s Sleep was restless (0,1)"

*value labels
label values r2sleepr cesd
label values s2sleepr cesd

***CESD: Was happy*****
*Wave 2 respondent Was happy
gen r2whappy =.
missing_common ac030_ , result(r2whappy)
replace r2whappy = .s if ac199_ == 2
replace r2whappy = 0 if ac030_==5
replace r2whappy = 1 if ac030_==1
label variable r2whappy "r2whappy:w2 CESD: r Was happy (0,1)"

*wave 2 spouse Was happy
gen s2whappy =.
spouse r2whappy, result(s2whappy) wave(2)
label variable s2whappy "s2whappy:w2 CESD: s Was happy (0,1)"

*value labels
label values r2whappy cesd
label values s2whappy cesd

***CESD: Felt lonely***
*Wave 2 respondent Felt lonely
gen r2flone =.
missing_common ac031_ , result(r2flone)
replace r2flone = .s if ac199_ == 2
replace r2flone = 0 if ac031_==5
replace r2flone = 1 if ac031_==1
label variable r2flone "r2flone:w2 CESD: r Felt lonely (0,1)"

*wave 2 spouse Felt lonely
gen s2flone =.
spouse r2flone, result(s2flone) wave(2)
label variable s2flone "s2flone:w2 CESD: s Felt lonely (0,1)"

*value labels
label values r2flone cesd
label values s2flone cesd

***CESD: Felt sad***
*Wave 2 respondent Felt sad
gen r2fsad =.
missing_common ac033_ , result(r2fsad)
replace r2fsad = .s if ac199_ == 2
replace r2fsad = 0 if ac033_==5
replace r2fsad = 1 if ac033_==1
label variable r2fsad "r2fsad:w2 CESD: r Felt sad (0,1)"

*wave 2 spouse Felt sad
gen s2fsad =.
spouse r2fsad, result(s2fsad) wave(2)
label variable s2fsad "s2fsad:w2 CESD: s Felt sad (0,1)"

*value labels
label values r2fsad cesd
label values s2fsad cesd

***CESD: Could not get going***
*Wave 2 respondent Could not get going
gen r2going =.
missing_common ac034_ , result(r2going)
replace r2going = .s if ac199_ == 2
replace r2going = 0 if ac034_==5
replace r2going = 1 if ac034_==1
label variable r2going "r2going:w2 CESD: r Could not get going (0,1)"

*wave 2 spouse Could not get going
gen s2going =.
spouse r2going, result(s2going) wave(2)
label variable s2going "s2going:w2 CESD: s Could not get going (0,1)"

*value labels
label values r2going cesd
label values s2going cesd

***CESD: Enjoyed life***
*Wave 2 respondent Enjoyed life
gen r2enlife =.
missing_common ac032_ , result(r2enlife)
replace r2enlife = .s if ac199_ == 2
replace r2enlife = 0 if ac032_==5
replace r2enlife = 1 if ac032_==1
label variable r2enlife "r2enlife:w2 CESD: r Enjoyed life (0,1)"

*wave 2 spouse Enjoyed life
gen s2enlife =.
spouse r2enlife, result(s2enlife) wave(2)
label variable s2enlife "s2enlife:w2 CESD: s Enjoyed life (0,1)"

*value labels
label values r2enlife cesd
label values s2enlife cesd

***CESD Score***
*wave 2 respondent CESD score number of missings
egen r2cesdm = rowmiss(r2depres r2effort r2sleepr r2flone r2fsad r2going r2whappy r2enlife) if inw2 == 1
label variable r2cesdm "r2cesdm:w2 r Missings in CESD score"

*wave 2 spouse CESD score number of missings
gen s2cesdm =.
spouse r2cesdm, result(s2cesdm) wave(2)
label variable s2cesdm "s2cesdm:w2 s Missings in CESD score"

local cesd
foreach v of varlist r2depres r2effort r2sleepr r2flone r2fsad r2going r2whappy r2enlife {
	recode `v' (miss = 0), generate(`v'_m)
	local cesd `cesd' `v'_m
}

*wave 2 respondent CESD score
gen r2cesd=.
missing_h r2depres r2effort r2sleepr r2flone r2fsad r2going r2whappy r2enlife, result(r2cesd)
replace r2cesd = .s if r2depres == .s & r2effort == .s & r2sleepr == .s & r2flone == .s & r2fsad == .s & r2going == .s & r2whappy == .s & r2enlife == .s
replace r2cesd = r2depres_m + r2effort_m + r2sleepr_m + r2flone_m + r2fsad_m + r2going_m + (1-r2whappy_m) + (1-r2enlife_m) if inrange(r2cesdm,0,7)
label variable r2cesd "r2cesd:w2 r CESD Score"

*wave 2 spouse CESD score
gen s2cesd =.
spouse r2cesd, result(s2cesd) wave(2)
label variable s2cesd "s2cesd:w2 s CESD Score"

*drop intermediate variables
drop `cesd'

***EURO-D: Depression***
*wave 2 respondent depression
gen r2depress =.
missing_common euro1, result(r2depress)
replace r2depress = euro1 if inlist(euro1,0,1)
label variable r2depress "r2depress:w2 EURO-D: r Depression (0,1)"
label values r2depress heurod

*wave 2 spouse depression
gen s2depress =.
spouse r2depress, result(s2depress) wave(2)
label variable s2depress "s2depress:w2 EURO-D: s Depression (0,1)"
label values s2depress heurod

***EURO-D: Pessimism***
*wave 2 respondent pessimism
gen r2pessim =.
missing_common euro2, result(r2pessim)
replace r2pessim = euro2 if inlist(euro2,0,1)
label variable r2pessim "r2pessim:w2 EURO-D: r Pessimism (0,1)"
label values r2pessim heurod

*wave 2 spouse pessimism
gen s2pessim =.
spouse r2pessim, result(s2pessim) wave(2)
label variable s2pessim "s2pessim:w2 EURO-D: s Pessimism (0,1)"
label values s2pessim heurod

***EURO-D: Suicidality***
*wave 2 respondent suicidality
gen r2suicid =.
missing_common euro3, result(r2suicid)
replace r2suicid = euro3 if inlist(euro3,0,1)
label variable r2suicid "r2suicid:w2 EURO-D: r Suicidality (0,1)"
label values r2suicid heurod

*wave 2 spouse suicidality
gen s2suicid =.
spouse r2suicid, result(s2suicid) wave(2)
label variable s2suicid "s2suicid:w2 EURO-D: s Suicidality (0,1)"
label values s2suicid heurod

***EURO-D: Guilt***
*wave 2 respondent guilt
gen r2guilt =.
missing_common euro4, result(r2guilt)
replace r2guilt = euro4 if inlist(euro4,0,1)
label variable r2guilt "r2guilt:w2 EURO-D: r Guilt (0,1)"
label values r2guilt heurod

*wave 2 spouse guilt
gen s2guilt =.
spouse r2guilt, result(s2guilt) wave(2)
label variable s2guilt "s2guilt:w2 EURO-D: s Guilt (0,1)"
label values s2guilt heurod

***EURO-D: Sleep***
*wave 2 respondent sleep
gen r2sleep =.
missing_common euro5, result(r2sleep)
replace r2sleep = euro5 if inlist(euro5,0,1)
label variable r2sleep "r2sleep:w2 EURO-D: r Sleep (0,1)"
label values r2sleep heurod

*wave 2 spouse sleep
gen s2sleep =.
spouse r2sleep, result(s2sleep) wave(2)
label variable s2sleep "s2sleep:w2 EURO-D: s Sleep (0,1)"
label values s2sleep heurod

***EURO-D: Interest***
*wave 2 respondent interest
gen r2intrst =.
missing_common euro6, result(r2intrst)
replace r2intrst = euro6 if inlist(euro6,0,1)
label variable r2intrst "r2intrst:w2 EURO-D: r Interest (0,1)"
label values r2intrst heurod

*wave 2 spouse interest
gen s2intrst =.
spouse r2intrst, result(s2intrst) wave(2)
label variable s2intrst "s2intrst:w2 EURO-D: s Interest (0,1)"
label values s2intrst heurod

***EURO-D: Irritability***
*wave 2 respondent irritability
gen r2irritb =.
missing_common euro7, result(r2irritb)
replace r2irritb = euro7 if inlist(euro7,0,1)
label variable r2irritb "r2irritb:w2 EURO-D: r Irritability (0,1)"
label values r2irritb heurod

*wave 2 spouse irritability
gen s2irritb =.
spouse r2irritb, result(s2irritb) wave(2)
label variable s2irritb "s2irritb:w2 EURO-D: s Irritability (0,1)"
label values s2irritb heurod

***EURO-D: Appetite***
*wave 2 respondent appetite
gen r2appett =.
missing_common euro8, result(r2appett)
replace r2appett = euro8 if inlist(euro8,0,1)
label variable r2appett "r2appett:w2 EURO-D: r Appetite (0,1)"
label values r2appett heurod

*wave 2 spouse appetite
gen s2appett =.
spouse r2appett, result(s2appett) wave(2)
label variable s2appett "s2appett:w2 EURO-D: s Appetite (0,1)"
label values s2appett heurod

***EURO-D: Fatigue***
*wave 2 respondent fatigue
gen r2fatig =.
missing_common euro9, result(r2fatig)
replace r2fatig = euro9 if inlist(euro9,0,1)
label variable r2fatig "r2fatig:w2 EURO-D: r Fatigue (0,1)"
label values r2fatig heurod

*wave 2 spouse fatigue
gen s2fatig =.
spouse r2fatig, result(s2fatig) wave(2)
label variable s2fatig "s2fatig:w2 EURO-D: s Fatigue (0,1)"
label values s2fatig heurod

***EURO-D: Concentration***
*wave 2 respondent concentration
gen r2concnt =.
missing_common euro10, result(r2concnt)
replace r2concnt = euro10 if inlist(euro10,0,1)
label variable r2concnt "r2concnt:w2 EURO-D: r Concentration (0,1)"
label values r2concnt heurod

*wave 2 spouse concentration
gen s2concnt =.
spouse r2concnt, result(s2concnt) wave(2)
label variable s2concnt "s2concnt:w2 EURO-D: s Concentration (0,1)"
label values s2concnt heurod

***EURO-D: Enjoyment***
*wave 2 respondent enjoyment
gen r2enjoym =.
missing_common euro11, result(r2enjoym)
replace r2enjoym = euro11 if inlist(euro11,0,1)
label variable r2enjoym "r2enjoym:w2 EURO-D: r Enjoyment (0,1)"
label values r2enjoym heurod

*wave 2 spouse enjoyment
gen s2enjoym =.
spouse r2enjoym, result(s2enjoym) wave(2)
label variable s2enjoym "s2enjoym:w2 EURO-D: s Enjoyment (0,1)"
label values s2enjoym heurod

***EURO-D: Tearfulness***
*wave 2 respondent tearfulness
gen r2tearfl =.
missing_common euro12, result(r2tearfl)
replace r2tearfl = euro12 if inlist(euro12,0,1)
label variable r2tearfl "r2tearfl:w2 EURO-D: r Tearfulness (0,1)"
label values r2tearfl heurod

*wave 2 spouse tearfulness
gen s2tearfl =.
spouse r2tearfl, result(s2tearfl) wave(2)
label variable s2tearfl "s2tearfl:w2 EURO-D: s Tearfulness (0,1)"
label values s2tearfl heurod

***EURO-D: Score***
*wave 2 respondent EURO-D score
gen r2eurod =.
replace r2eurod = eurod
label variable r2eurod "r2eurod:w2 EURO-D: r Score"

*wave 2 respondent flag for EURO-D score
gen r2feurod = .
replace r2feurod = 1 if eurod_f==3
replace r2feurod = 5 if eurod_f==14
label variable r2feurod "r2feurod:w2 EURO-D Flag: r Score"
label values r2feurod healthimpflag

*wave 2 spouse EURO-D score
gen s2eurod =.
spouse r2eurod, result(s2eurod) wave(2)
label variable s2eurod "s2eurod:w2 EURO-D: s Score"

*wave 2 spouse flag for EURO-D score
gen s2feurod =.
spouse r2feurod, result(s2feurod) wave(2)
label variable s2feurod "s2feurod:w2 EURO-D Flag: s Score"
label values s2feurod healthimpflag

***Ever had high blood pressure***
*wave 2 respondent ever had high blood pressure
gen r2hibpe =.
missing_common ph006d2, result(r2hibpe)
replace r2hibpe = 0 if ph006d2 == 0
replace r2hibpe = 1 if (ph006d2 == 1 | r`pre_wv'hibpe == 1) & inw`wv' == 1
label variable r2hibpe "r2hibpe:w2 r ever had high blood pressure"
label values r2hibpe health_problems

*wave 2 spouse ever had high blood pressure
gen s2hibpe =.
spouse r2hibpe, result(s2hibpe) wave(2)
label variable s2hibpe "s2hibpe:w2 s ever had high blood pressure"
label values s2hibpe health_problems

***Ever had diabetes***
*wave 2 respondent ever had diabetes
gen r2diabe =.
missing_common ph006d5, result(r2diabe)
replace r2diabe = 0 if ph006d5 == 0 
replace r2diabe = 1 if (ph006d5 == 1 | r`pre_wv'diabe == 1) & inw`wv' == 1
label variable r2diabe "r2diabe:w2 r ever had diabetes"
label values r2diabe health_problems

*wave 2 spouse ever had diabetes
gen s2diabe =.
spouse r2diabe, result(s2diabe) wave(2)
label variable s2diabe "s2diabe:w2 s ever had diabetes"
label values s2diabe health_problems

***Ever had cancer***
*wave 2 respondent ever had cancer
gen r2cancre =.
missing_common ph006d10 ph067_1, result(r2cancre)
replace r2cancre = 0 if ph006d10 == 0
replace r2cancre = 1 if (ph006d10 == 1 | ph067_3 == 1 | r`pre_wv'cancre == 1) & inw`wv' == 1
label variable r2cancre "r2cancre:w2 r ever had cancer"
label values r2cancre health_problems

*wave 2 respondent cancer dispute flag
gen r`wv'cancrf = .
replace r`wv'cancrf = 0 if inlist(ph006d10,0,1) | ph067_3 == 1 | (r`pre_wv'cancre == 1 & inw`wv' == 1)
replace r`wv'cancrf = 2 if ph068_3 == 5 & r`pre_wv'cancre == 1
replace r`wv'cancrf = 3 if ph068_3 == 5 & r`pre_wv'cancre == 0
label variable r`wv'cancrf "r`wv'cancrf:w`wv' r cancer dispute flag"
label values r`wv'cancrf ehealth_problems_flag

*update wave 1 dispute flag
replace r`pre_wv'cancrf = 5 if ph068_3 == 5 & r`pre_wv'cancre == 1 & r`pre_wv'cancrf == 0
replace r`pre_wv'cancrf = 6 if ph068_3 == 5 & r`pre_wv'cancre == 0 & r`pre_wv'cancrf == 0

*update wave 1 ever have varible
replace r`pre_wv'cancre = 0 if ph068_3 == 5 & r`pre_wv'cancre == 1

***Ever had lung disease***
*wave 2 respondent ever had lung disease
gen r2lunge =.
missing_common ph006d6, result(r2lunge)
replace r2lunge = 0 if ph006d6 == 0 
replace r2lunge = 1 if (ph006d6 == 1 | r`pre_wv'lunge == 1)  & inw`wv' == 1
label variable r2lunge "r2lunge:w2 r ever had lung disease"
label values r2lunge health_problems

*wave 2 spouse ever had lung disease
gen s2lunge =.
spouse r2lunge, result(s2lunge) wave(2)
label variable s2lunge "s2lunge:w2 s ever had lung disease"
label values s2lunge health_problems

***Ever had heart problems***
*wave 2 respondent ever had heart problems
gen r2hearte =.
missing_common ph006d1, result(r2hearte)
replace r2hearte = 0 if ph006d1 == 0
replace r2hearte = 1 if (ph006d1 == 1 | ph067_1 == 1 | r`pre_wv'hearte == 1) & inw`wv' == 1
label variable r2hearte "r2hearte:w2 r ever had heart problems"
label values r2hearte health_problems

*wave 2 respondent heart problems dispute flag
gen r`wv'heartf = .
replace r`wv'heartf = 0 if inlist(ph006d1,0,1) | ph067_1 == 1 | (r`pre_wv'hearte == 1 & inw`wv' == 1)
replace r`wv'heartf = 2 if ph068_1 == 5 & r`pre_wv'hearte == 1
replace r`wv'heartf = 3 if ph068_1 == 5 & r`pre_wv'hearte == 0
label variable r`wv'heartf "r`wv'heartf:w`wv' r heart problems dispute flag"
label values r`wv'heartf ehealth_problems_flag

*update wave 1 dispute flag
replace r`pre_wv'heartf = 5 if ph068_1 == 5 & r`pre_wv'hearte == 1 & r`pre_wv'heartf == 0
replace r`pre_wv'heartf = 6 if ph068_1 == 5 & r`pre_wv'hearte == 0 & r`pre_wv'heartf == 0

*update wave 1 ever have varible
replace r`pre_wv'hearte = 0 if ph068_1 == 5 & r`pre_wv'hearte == 1

***Ever had stroke***
*wave 2 respondent ever had stroke
gen r2stroke =.
missing_common ph006d4 , result(r2stroke)
replace r2stroke = 0 if ph006d4 == 0
replace r2stroke = 1 if (ph006d4 == 1 | ph067_2 == 1 | r`pre_wv'stroke == 1) & inw`wv' == 1
label variable r2stroke "r2stroke:w2 r ever had stroke"
label values r2stroke health_problems

*wave 2 respondent stroke dispute flag
gen r`wv'strokf = .
replace r`wv'strokf = 0 if inlist(ph006d4,0,1) | ph067_2 == 1 | (r`pre_wv'stroke == 1 & inw`wv' == 1)
replace r`wv'strokf = 2 if ph068_2 == 5 & r`pre_wv'stroke == 1
replace r`wv'strokf = 3 if ph068_2 == 5 & r`pre_wv'stroke == 0
label variable r`wv'strokf "r`wv'strokf:w`wv' r stroke dispute flag"
label values r`wv'strokf ehealth_problems_flag

*update wave 1 dispute flag
replace r`pre_wv'strokf = 5 if ph068_2 == 5 & r`pre_wv'stroke == 1 & r`pre_wv'strokf == 0
replace r`pre_wv'strokf = 6 if ph068_2 == 5 & r`pre_wv'stroke == 0 & r`pre_wv'strokf == 0

*update wave 1 ever have varible
replace r`pre_wv'stroke = 0 if ph068_2 == 5 & r`pre_wv'stroke == 1

***Ever had arthritis***
*wave 2 respondent ever had arthritis
gen r2arthre =.
missing_common ph006d8 , result(r2arthre)
replace r2arthre = 0 if ph006d8 == 0
replace r2arthre = 1 if (ph006d8 == 1 | r`pre_wv'arthre == 1) & inw`wv' == 1
label variable r2arthre "r2arthre:w2 r ever had arthritis"
label values r2arthre health_problems

*wave 2 spouse ever had arthritis
gen s2arthre =.
spouse r2arthre, result(s2arthre) wave(2)
label variable s2arthre "s2arthre:w2 s ever had arthritis"
label values s2arthre health_problems

***Ever had asthma***
*wave 2 respondent ever had asthma
gen r2asthmae = .
missing_common ph006d7, result(r2asthmae)
replace r2asthmae = 0 if ph006d7 == 0
replace r2asthmae = 1 if (ph006d7 == 1 | r`pre_wv'asthmae == 1) & inw`wv' == 1
label variable r2asthmae "r2asthmae:w2 r ever had asthma"
label values r2asthmae health_problems

*wave 2 spouse ever had asthma
gen s2asthmae = .
spouse r2asthmae, result(s2asthmae) wave(2)
label variable s2asthmae "s2asthmae:w2 s ever had asthma"
label values s2asthmae health_problems

***Ever had high cholesterol***
*wave 2 respondent ever had high cholesterol
gen r2hchole = .
missing_common ph006d3, result(r2hchole)
replace r2hchole = 0 if ph006d3 == 0
replace r2hchole = 1 if (ph006d3 == 1 | r`pre_wv'hchole == 1) & inw`wv' == 1
label variable r2hchole "r2hchole:w2 r ever had high cholesterol"
label values r2hchole health_problems

*wave 2 spouse ever had high cholesterol
gen s2hchole = .
spouse r2hchole, result(s2hchole) wave(2)
label variable s2hchole "s2hchole:w2 s ever had high cholesterol"
label values s2hchole health_problems

***Ever had Parkinsons***
*wave 2 respondent ever had parkinsons
gen r2parkine = .
missing_common ph006d12, result(r2parkine)
replace r2parkine = 0 if ph006d12 == 0
replace r2parkine = 1 if (ph006d12 == 1 | r`pre_wv'parkine == 1) & inw`wv' == 1
label variable r2parkine "r2parkine:w2 r ever had parkinson disease"
label values r2parkine health_problems

*wave 2 spouse ever had parkinsons
gen s2parkine = .
spouse r2parkine, result(s2parkine) wave(2)
label variable s2parkine "s2parkine:w2 s ever had parkinson disease"
label values s2parkine health_problems

***Ever had cataracts***
*wave 2 respondent ever had cataracts
gen r2catrcte = .
missing_common ph006d13, result(r2catrcte)
replace r2catrcte = 0 if ph006d13 == 0
replace r2catrcte = 1 if (ph006d13 == 1 | r`pre_wv'catrcte == 1) & inw`wv' == 1
label variable r2catrcte "r2catrcte:w2 r ever had cataracts"
label values r2catrcte health_problems

*wave 2 spouse ever had cataracts
gen s2catrcte = .
spouse r2catrcte, result(s2catrcte) wave(2)
label variable s2catrcte "s2catrcte:w2 s ever had cataracts"
label values s2catrcte health_problems

***Ever had hip or femoral fracture***
*wave 2 respondent ever had hip or femoral fracture
gen r2hipfeme = .
missing_common ph006d14, result(r2hipfeme)
replace r2hipfeme = 0 if ph006d14 == 0
replace r2hipfeme = 1 if (ph006d14 == 1 | ph067_4 == 1 | r`pre_wv'hipfeme == 1) & inw`wv' == 1
label variable r2hipfeme "r2hipfeme:w2 r ever had hip/femoral fracture"
label values r2hipfeme health_problems

*wave 2 respondent hip/femoral fracture dispute flag
gen r`wv'hipfemf = .
replace r`wv'hipfemf = 0 if inlist(ph006d14,0,1) | ph067_4 == 1 | (r`pre_wv'hipfeme == 1 & inw`wv' == 1)
replace r`wv'hipfemf = 2 if ph068_4 == 5 & r`pre_wv'hipfeme == 1
replace r`wv'hipfemf = 3 if ph068_4 == 5 & r`pre_wv'hipfeme == 0
label variable r`wv'hipfemf "r`wv'hipfemf:w`wv' r hip/femoral fracture dispute flag"
label values r`wv'hipfemf ehealth_problems_flag

*update wave 1 respondent hip/femoral fracture dispute flag
replace r`pre_wv'hipfemf = 5 if ph068_4 == 5 & r`pre_wv'hipfeme == 1 & r`pre_wv'hipfemf == 0
replace r`pre_wv'hipfemf = 6 if ph068_4 == 5 & r`pre_wv'hipfeme == 0 & r`pre_wv'hipfemf == 0

*update wave 1 respondent ever have variable
replace r`pre_wv'hipfeme = 0 if ph068_4 == 5 & r`pre_wv'hipfeme == 1

***Ever had psychiatric disorder***
*wave 2 respondent ever had psych disorder
gen r2psyche = .
missing_common mh022, result(r2psyche)
replace r2psyche = 0 if mh022 == 5
replace r2psyche = 1 if mh022 == 1
label variable r2psyche "r2psyche:w2 r ever had psych disorder"
label values r2psyche health_problems

*wave 2 spouse ever had psych disorder
gen s2psyche = .
spouse r2psyche, result(s2psyche) wave(2)
label variable s2psyche "s2psyche:w2 s ever had psych disorder"
label values s2psyche health_problems

***Ever had ulcer***
*wave 2 respondent ever had ulcer
gen r2ulcer =.
missing_common ph006d11, result(r2ulcer)
replace r2ulcer = 0 if ph006d11 == 0
replace r2ulcer = 1 if (ph006d11 == 1 | r`pre_wv'ulcer == 1) & inw`wv' == 1
label variable r2ulcer "r2ulcer:w2 r ever had ulcer"
label values r2ulcer health_problems

*wave 2 spouse ever had ulcer
gen s2ulcer =.
spouse r2ulcer, result(s2ulcer) wave(2)
label variable s2ulcer "s2ulcer:w2 s ever had ulcer"
label values s2ulcer health_problems

***Ever had Alzheimerís disease, dementia***
*wave 2 respondent ever had alzheimerís or dementia
gen r2alzdem =.
missing_common ph006d16 , result(r2alzdem)
replace r2alzdem = 0 if ph006d16 == 0
replace r2alzdem = 1 if ph006d16 == 1
label variable r2alzdem "r2alzdem:w2 r ever had alzheimer's/dementia"
label values r2alzdem health_problems

*wave 2 spouse ever had alzheimerís or dementia
gen s2alzdem =.
spouse r2alzdem, result(s2alzdem) wave(2)
label variable s2alzdem "s2alzdem:w2 s ever had alzheimer's/dementia"
label values s2alzdem health_problems

***Height in meters***
*wave 2 respondent height in meters
gen r2height =.
missing_common ph013_, result(r2height)
replace r2height = r1height if !mi(r1height) & inw2==1
replace r2height = .i if inrange(ph013_,0,89) 
replace r2height = ph013_ if inrange(ph013_,1.00,1.90) & inw2==1
replace r2height = ph013_/100 if inrange(ph013_,90,220) & inw2==1
label variable r2height "r2height:w2 r Height in meters"

*wave 2 spouse height in meters
gen s2height =.
spouse r2height, result(s2height) wave(2)
label variable s2height "s2height:w2 s Height in meters"

***Weight in kilograms***
*wave 2 respondent weight in kilograms
gen r2weight =.
missing_common ph012_, result(r2weight)
replace r2weight = .i if inrange(ph012_,0,12) 
replace r2weight = ph012_ if inrange(ph012_,13,250) 
label variable r2weight "r2weight:w2 r Weight in kilograms"

*wave 2 spouse weight in kilograms
gen s2weight =.
spouse r2weight, result(s2weight) wave(2)
label variable s2weight "s2weight:w2 s Weight in kilograms"

***BMI***
*wave 2 respondent BMI
gen r2bmi =.
missing_h r2weight r2height, result(r2bmi)
replace r2bmi = r2weight/(r2height^2) if !mi(r2weight) & !mi(r2height)
replace r2bmi = .i if (r2weight == .i | r2height == .i | !inrange(r2bmi,12,100)) & inw`wv' == 1
label variable r2bmi "r2bmi:w2 r Body Mass Index=kg/m2"

*wave 2 spouse BMI
gen s2bmi =.
spouse r2bmi, result(s2bmi) wave(2)
label variable s2bmi "s2bmi:w2 s Body Mass Index=kg/m2"

***BMI Categorization***
*wave 2 respondent BMI categorization
gen r2bmicat = .
missing_h r2weight r2height, result(r2bmicat)
replace r2bmicat = 1 if inrange(r2bmi,1,18.49999)
replace r2bmicat = 2 if inrange(r2bmi,18.50,24.99999)
replace r2bmicat = 3 if inrange(r2bmi,25,29.99999)
replace r2bmicat = 4 if inrange(r2bmi,30,34.99999)
replace r2bmicat = 5 if inrange(r2bmi,35,39.99999)
replace r2bmicat = 6 if inrange(r2bmi,40,100)
replace r2bmicat = .i if r2weight == .i | r2height == .i | r2bmi==.i
label variable r2bmicat "r2bmicat:w2 r BMI categorization"
label values r2bmicat bmicate

*wave 2 spouse BMI categorization
gen s2bmicat = .
spouse r2bmicat, result(s2bmicat) wave(2)
label variable s2bmicat "s2bmicat:w2 s BMI categorization"
label values s2bmicat bmicate

***Vigorous physical activity***
*wave 2 respondent vigorous physical activity
gen r2vgactx =.
missing_common br015_, result(r2vgactx)
replace r2vgactx = 2 if br015_ == 1
replace r2vgactx = 3 if br015_ == 2
replace r2vgactx = 4 if br015_ == 3
replace r2vgactx = 5 if br015_ == 4
label variable r2vgactx "r2vgactx:w2 R Freq vigorous phys activ"

*wave 2 spouse vigorous physical activity
gen s2vgactx =.
spouse r2vgactx, result(s2vgactx) wave(2)
label variable s2vgactx "s2vgactx:w2 S Freq vigorous phys activ"

*label values
label values r2vgactx activity
label values s2vgactx activity

***Moderate physical activity***
*wave 2 respondent moderate physical activity
gen r2mdactx =.
missing_common br016_, result(r2mdactx)
replace r2mdactx = 2 if br016_ == 1
replace r2mdactx = 3 if br016_ == 2
replace r2mdactx = 4 if br016_ == 3
replace r2mdactx = 5 if br016_ == 4
label variable r2mdactx "r2mdactx:w2 R Freq moderate phys activ"

*wave 2 spouse vigorous moderate activity
gen s2mdactx =.
spouse r2mdactx, result(s2mdactx) wave(2)
label variable s2mdactx "s2mdactx:w2 S Freq moderate phys activ"

*label values
label values r2mdactx activity
label values s2mdactx activity

***Ever drinks alcohol***
**change to last 7 days
*wave 2 respondent ever drinks alcohol
gen r2drink =.
missing_common br010_, result(r2drink)
replace r2drink = 0 if inlist(br010_,5,6,7)
replace r2drink = 1 if inlist(br010,1,2,3,4)
label variable r2drink "r2drink:w2 R ever drinks any alcohol"

*wave 2 spouse ever drinks alcohol
gen s2drink =.
spouse r2drink, result(s2drink) wave(2)
label variable s2drink "s2drink:w2 S ever drinks any alcohol"

*label values
label values r2drink ever_drink
label values s2drink ever_drink

***Days per week drinks***
*wave 2 respondent days per week drinks
gen r2drinkx =.
missing_common br010_, result(r2drinkx)
replace r2drinkx = 0 if inlist(br010_,6,7)
replace r2drinkx = 1 if br010_ == 5
replace r2drinkx = 2 if inlist(br010_,4,3) 
replace r2drinkx = 3 if br010_ == 2 
replace r2drinkx = 4 if br010_ == 1 
label variable r2drinkx "r2drinkx:w2 R Frequency of drinking"
label values r2drinkx drinkx

*wave 2 spouse days per week drinks
gen s2drinkx =.
spouse r2drinkx, result(s2drinkx) wave(1)
label variable s2drinkx "s2drinkx:w2 S Frequency of drinking"
label values s2drinkx drinkx

***No. Drinks per day when drinks***
*wave 2 respondent no. drinks per day when drinks
gen r2drinkn = .
missing_common br019_, result(r2drinkn)
replace r2drinkn = br019_ if inrange(br019_,0,70)
replace r2drinkn = 0 if br010_ == 7
label variable r2drinkn "r2drinkn:w2 r no. drinks per day"

*wave 2 spouse no. drinks per day when drinks
gen s2drinkn = .
spouse r2drinkn, result(s2drinkn) wave(2)
label variable s2drinkn "s2drinkn:w2 s no. drinks per day"

***Ever smoked cigarettes***
*wave 2 respondent ever smoked cigarettes
gen r2smokev =.
missing_common br001_ br002_, result(r2smokev)
replace r2smokev = r1smokev if !mi(r1smokev) & inw2 == 1
replace r2smokev = 0 if br001_ == 5
replace r2smokev = 1 if br001_ == 1 | br002_ == 1
label variable r2smokev "r2smokev:w2 R smoke ever"

*wave 2 spouse ever smoked cigarettes
gen s2smokev =.
spouse r2smokev, result(s2smokev) wave(2)
label variable s2smokev "s2smokev:w2 S smoke ever"

*label values
label values r2smokev smokes
label values s2smokev smokes

***Smokes cigarettes now***
*wave 2 respondent smokes cigarettes now
***check br022_ 
gen r2smoken =.
missing_common br001_ br002_, result(r2smoken)
replace r2smoken = 0 if br001_ == 5 | (br002_ == 5 & br022_ != 3)
replace r2smoken = 1 if br002_ == 1 | br022_ == 3
label variable r2smoken "r2smoken:w2 R smokes now"

*wave 2 spouse smokes cigarettes now
gen s2smoken =.
spouse r2smoken, result(s2smoken) wave(2)
label variable s2smoken "s2smoken:w2 S smokes now"

*label values
label values r2smoken smokes
label values s2smoken smokes

***Number cigarettes/pipe/cigar per day when smokes***
*wave 2 respondent number cigarettes/pipe/cigar per day when smokes
gen br006n = br006_ if inrange(br006_,0,120)
gen br007n = br007_ if inrange(br007_,0,120)
gen br008n = br008_ if inrange(br008_,0,120)
egen r2smokef = rowtotal (br006n br007n br008n), m
replace r2smokef = .m if (br006_ == . & br007_ == . & br008_ == .) & inw`wv' == 1
replace r2smokef = .d if inlist(r2smokef,.,.m) & (br006_ == -1 | br007_ == -1 | br008_ == -1)
replace r2smokef = .r if inlist(r2smokef,.,.m) & (br006_ == -2 | br007_ == -2 | br008_ == -2)
replace r2smokef = 0 if br001_ == 5 | (br001_ == 1 & br002_ == 5)
replace r2smokef = r1smokef if !mi(r1smokef) & inw2==1 & r2smokef==.m
label variable r2smokef "r2smokef:w2 r amount smokes per day"

*wave 2 spouse number cigarettes/pipe/cigar per day when smokes
gen s2smokef = .
spouse r2smokef, result(s2smokef) wave(2)
label variable s2smokef "s2smokef:w2 s amount smokes per day"

drop br006n br007n br008n


***drop wave 2 Behavioural Risk file raw variables
drop `health_w2_br' 

***drop wave 2 Mental Health file raw variables
drop `health_w2_mh'

***drop wave 2 Physical Health file raw variables
drop `health_w2_ph'

***drop wave 2 Activities file raw variables
drop `health_w2_ac'

***drop wave 2 Generated Variables (Health) file raw variables
drop `health_w2_gv_health'

***drop wave 2 imputation file raw variables***
drop `health_w2_imput'

******************************************************************************************


*set wave number
local wv = 4
local pre_wv = 2
local wvs_before 2 1

***merge with wave 4 Behavioural Risk data***
local health_w4_br br015_ br016_ br010_ br001_ br002_ br022_ br019_ 
merge 1:1 mergeid using "$wave_4_br", keepusing(`health_w4_br') nogen

***merge with wave 4 Mental Health data***
local health_w4_mh mh020_ mh018_ mh022
merge 1:1 mergeid using "$wave_4_mh", keepusing(`health_w4_mh') nogen

***merge with wave 4 Physical Health data***
local health_w4_ph ph003_ ph005_ ph049d2 ph049d1 ph049d3 ///
					ph049d4 ph049d5 ph049d6 ph049d7 ph049d10 ph049d13 ph049d11 ph049d12 ///
					ph049d9 ph049d8 ph048d1 ph048d2 ph048d3 ph048d4 ph048d5 ph048d6 ///
					ph048d9 ph048d10 ph048d7 ph048d8 ph006d2 ph006d3 ph006d5 ph006d10 ///
					ph006d6 ph006d1 ph006d4 ph006d8 ph006d12 ph006d13 ph006d14 ///
					ph013_ ph012_ ph006d11 ph006d16 ph072_? ph073_?
merge 1:1 mergeid using "$wave_4_ph", keepusing(`health_w4_ph') nogen

***merge with wave 4 Generated Variables (Health) data***
local health_w4_gv_health euro1 euro2 euro3 euro4 euro5 euro6 euro7 ///
                          euro8 euro9 euro10 euro11 euro12
merge 1:1 mergeid using "$wave_4_gv_health", keepusing(`health_w4_gv_health') nogen 

***merge with wave 4 imputation data***
local health_w4_imput eurod eurod_f
merge 1:1 mergeid using "$wave_4_imputations_", keepusing(`health_w4_imput') nogen


***self-report of health***
*wave 4 respondnet self-report of health
gen r4shlt =.
missing_common ph003_ , result(r4shlt)
replace r4shlt = 1 if ph003_==1
replace r4shlt = 2 if ph003_==2
replace r4shlt = 3 if ph003_==3
replace r4shlt = 4 if ph003_==4 
replace r4shlt = 5 if ph003_==5 
label variable r4shlt "r4shlt:w4 r self-report of health"
label values r4shlt health

*wave 4 spouse self-report of health
gen s4shlt =.
spouse r4shlt, result(s4shlt) wave(4)
label variable s4shlt "s4shlt:w4 s self-report of health"
label values s4shlt health

***Whether health limits activities***
*wave 4 respodnet whether health limits activites
gen r4hlthlma =.
missing_common ph005_, result(r4hlthlma)
replace r4hlthlma = 0 if ph005_ == 3
replace r4hlthlma = 1 if inrange(ph005_,1,2)
label variable r4hlthlma "r4hlthlma:w4 r hlth problem limits activities"
label values r4hlthlma limits

*wave 4 spouse whether healh limits activities
gen s4hlthlma =.
spouse r4hlthlma, result(s4hlthlma) wave(4)
label variable s4hlthlma "s4hlthlma:w4 s hlth problem limits activities"
label values s4hlthlma limits

***Some difficulty walking cross room***
*wave 4 respondent difficulty walking across room
gen r4walkra =.
missing_common ph049d2, result(r4walkra)	
replace r4walkra = 0 if ph049d2==0 
replace r4walkra = 1 if ph049d2==1 
label variable r4walkra "r4walkra:w4 R Some Diff-Walk across room"
label values r4walkra adldiff

*wave 4 spouse difficulty walking across room
gen s4walkra =.
spouse r4walkra, result(s4walkra) wave(4)
label variable s4walkra "s4walkra:w4 S Some Diff-Walk across room"
label values s4walkra adldiff

***Some difficulty dressing***
*wave 4 respondent difficulty dressing
gen r4dressa =.
missing_common ph049d1 , result(r4dressa)		
replace r4dressa = 0 if ph049d1==0 
replace r4dressa = 1 if ph049d1==1 
label variable r4dressa "r4dressa:w4 R Some Diff-Dressing"
label values r4dressa adldiff

*wave 4 spouse difficulty dressing
gen s4dressa =.
spouse r4dressa, result(s4dressa) wave(4)
label variable s4dressa "s4dressa:w4 S Some Diff-Dressing"
label values s4dressa adldiff

***Some difficulty bathing, shower***
*wave 4 respondent difficulty bathing, shower
gen r4batha =.
missing_common ph049d3 , result(r4batha)		
replace r4batha = 0 if ph049d3==0 
replace r4batha = 1 if ph049d3==1 
label variable r4batha "r4batha:w4 R Some Diff-Bathing, shower"
label values r4batha adldiff

*wave 4 spouse difficulty bathing, shower
gen s4batha =.
spouse r4batha, result(s4batha) wave(4)
label variable s4batha "s4batha:w4 S Some Diff-Bathing, shower"
label values s4batha adldiff

***Some difficulty eating***
*wave 4 respondent difficulty eating
gen r4eata =.
missing_common ph049d4 , result(r4eata)
replace r4eata = 0 if ph049d4==0 
replace r4eata = 1 if ph049d4==1 
label variable r4eata "r4eata:w4 R Some Diff-Eating"
label values r4eata adldiff

*wave 4 spouse difficulty eating
gen s4eata =.
spouse r4eata, result(s4eata) wave(4)
label variable s4eata "s4eata:w4 S Some Diff-Eating"
label values s4eata adldiff

***Some difficulty getting in/out of bed***
*wave 4 respondent difficulty getting in/out of bed
gen r4beda =.
missing_common ph049d5 , result(r4beda)
replace r4beda = 0 if ph049d5==0  
replace r4beda = 1 if ph049d5==1 
label variable r4beda "r4beda:w4 R Some Diff-Get in/out bed"
label values r4beda adldiff

*wave 4 spouse difficulty getting in/out of bed
gen s4beda =.
spouse r4beda, result(s4beda) wave(4)
label variable s4beda "s4beda:w4 S Some Diff-Get in/out bed"
label values s4beda adldiff


***Some difficulty using the toilet***
*wave 4 respondent difficulty using the toilet
gen r4toilta =.
missing_common ph049d6 , result(r4toilta)			
replace r4toilta = 0 if ph049d6==0  
replace r4toilta = 1 if ph049d6==1 
label variable r4toilta "r4toilta:w4 R Some Diff-Using the toilet"
label values r4toilta adldiff

*wave 4 spouse difficulty using the toilet
gen s4toilta =.
spouse r4toilta, result(s4toilta) wave(4)
label variable s4toilta "s4toilta:w4 S Some Diff-Using the toilet"
label values s4toilta adldiff

***Some difficulty using using a map***
*wave 4 respondent difficulty using a map
gen r4mapa =.
missing_common ph049d7 , result(r4mapa)	
replace r4mapa = 0 if ph049d7==0 
replace r4mapa = 1 if ph049d7==1  
label variable r4mapa "r4mapa:w4 R Some Diff-Use a map"
label values r4mapa adldiff

*wave 4 spouse difficulty using a map
gen s4mapa =.
spouse r4mapa, result(s4mapa) wave(4)
label variable s4mapa "s4mapa:w4 S Some Diff-Use a map"
label values s4mapa adldiff

***Some difficulty using a telephone***
*wave 4 respondent difficulty using a telephone
gen r4phonea =.
missing_common ph049d10, result(r4phonea)			
replace r4phonea = 0 if ph049d10==0 
replace r4phonea = 1 if ph049d10==1 
label variable r4phonea "r4phonea:w4 R Some Diff-Use telephone"
label values r4phonea adldiff

*wave 4 spouse difficulty using a telephone
gen s4phonea =.
spouse r4phonea, result(s4phonea) wave(4)
label variable s4phonea "s4phonea:w4 S Some Diff-Use a telephone"
label values s4phonea adldiff

***Some difficulty manging money***
*wave 4 respondent difficulty manging money
gen r4moneya =.
missing_common ph049d13 , result(r4moneya)				
replace r4moneya = 0 if ph049d13==0 
replace r4moneya = 1 if ph049d13==1
label variable r4moneya "r4moneya:w4 R Some Diff-Managing money"
label values r4moneya adldiff

*wave 4 spouse difficulty manging money
gen s4moneya =.
spouse r4moneya, result(s4moneya) wave(4)
label variable s4moneya "s4moneya:w4 S Some Diff-Managing money"
label values s4moneya adldiff

***Some difficulty taking medications***
*wave 4 respondent difficulty taking medications
gen r4medsa =.
missing_common ph049d11 , result(r4medsa)					
replace r4medsa = 0 if ph049d11==0 
replace r4medsa = 1 if ph049d11==1 
label variable r4medsa "r4medsa:w4 R Some Diff-Take medications"
label values r4medsa adldiff

*wave 4 spouse difficulty taking medications
gen s4medsa =.
spouse r4medsa, result(s4medsa) wave(4)
label variable s4medsa "s4medsa:w4 S Some Diff-Take medications"
label values s4medsa adldiff

***Some difficulty shopping for groceries***
*wave 4 respondent difficulty shopping for groceries
gen r4shopa =.
missing_common ph049d9, result(r4shopa)			
replace r4shopa = 0 if ph049d9==0 
replace r4shopa = 1 if ph049d9==1 
label variable r4shopa "r4shopa:w4 R Some Diff-Shop for grocery"
label values r4shopa adldiff

*wave 1 spouse difficulty shopping for groceries
gen s4shopa =.
spouse r4shopa, result(s4shopa) wave(4)
label variable s4shopa "s4shopa:w4 S Some Diff-Shop for grocery"
label values s4shopa adldiff

***Some difficulty preparing a hot meal***
*wave 4 respondent difficulty preparing a hot meal
gen r4mealsa =.
missing_common ph049d8 , result(r4mealsa)
replace r4mealsa = 0 if ph049d8==0  
replace r4mealsa = 1 if ph049d8==1 
label variable r4mealsa "r4mealsa:w4 R Some Diff-Prepare hot meal"
label values r4mealsa adldiff

*wave 4 spouse difficulty preparing a hot meal
gen s4mealsa =.
spouse r4mealsa, result(s4mealsa) wave(4)
label variable s4mealsa "s4mealsa:w4 S Some Diff-Prepare hot meal"
label values s4mealsa adldiff

***Some difficulty doing work around the house***
*wave 4 respondent difficulty doing work around the house
gen r4housewka = .
missing_common ph049d12, result(r4housewka)
replace r4housewka = 0 if ph049d12==0
replace r4housewka = 1 if ph049d12==1
label variable r4housewka "r4housewka:w4 R some diff-doing work around house"
label values r4housewka adldiff

*wave 4 spouse difficulty doing work around the house
gen s4housewka = .
spouse r4housewka, result(s4housewka) wave(4)
label variable s4housewka "s4housewka:w4 s some diff-doing work around house"
label values s4housewka adldiff

***Difficulty Walking 100m***
*wave 4 respondent difficulty walking 100m
gen r4walk100a =.
missing_common ph048d1 , result(r4walk100a)
replace r4walk100a = 0 if ph048d1==0  
replace r4walk100a = 1 if ph048d1==1 
label variable r4walk100a "r4walk100a:w4 R Some Diff-Walk 100m"
label values r4walk100a adldiff

*wave 4 spouse difficulty walking 100m
gen s4walk100a =.
spouse r4walk100a, result(s4walk100a) wave(4)
label variable s4walk100a "s4walk100a:w4 S Some Diff-Walk 100m"
label values s4walk100a adldiff

***Difficulty sitting for 2 hours***
*wave 4 respondent difficulty sitting for 2 hours
gen r4sita =.
missing_common ph048d2 , result(r4sita)
replace r4sita = 0 if ph048d2==0
replace r4sita = 1 if ph048d2==1 
label variable r4sita "r4sita:w4 R Some Diff-Sit for 2 hours"
label values r4sita adldiff

*wave 1 spouse difficulty sitting for 2 hours
gen s4sita =.
spouse r4sita, result(s4sita) wave(4)
label variable s4sita "s4sita:w4 S Some Diff-Sit for 2 hours"
label values s4sita adldiff

***Difficulty getting up from a chair***
*wave 4 respondent difficulty getting up from a chair
gen r4chaira =.
missing_common ph048d3  , result(r4chaira)	
replace r4chaira = 0 if ph048d3==0 
replace r4chaira = 1 if ph048d3==1
label variable r4chaira "r4chaira:w4 R Some Diff-Get up fr chair"
label values r4chaira adldiff

*wave 4 spouse difficulty getting up from a chair
gen s4chaira =.
spouse r4chaira, result(s4chaira) wave(4)
label variable s4chaira "s4chaira:w4 S Some Diff-Get up fr chair"
label values s4chaira adldiff

***Difficulty climbing several flights of stairs***
*wave 4 respondent difficulty climbing several flights of stairs
gen r4climsa =.
missing_common ph048d4 , result(r4climsa)
replace r4climsa = 0 if ph048d4==0 
replace r4climsa = 1 if ph048d4==1 
label variable r4climsa "r4climsa:w4 R Some Diff-Clmb sev flt str"
label values r4climsa adldiff

*wave 4 spouse difficulty climbing several flights of stairs
gen s4climsa =.
spouse r4climsa, result(s4climsa) wave(4)
label variable s4climsa "s4climsa:w4 S Some Diff-Clmb sev flt str"
label values s4climsa adldiff

***Difficulty climbing one flight of stairs***
*wave 4 respondent difficulty climbing one flight of stairs
gen r4clim1a =.
missing_common ph048d5 , result(r4clim1a)		
replace r4clim1a = 0 if ph048d5==0 
replace r4clim1a = 1 if ph048d5==1
label variable r4clim1a "r4clim1a:w4 R Some Diff-Clmb 1 flt str"
label values r4clim1a adldiff

*wave 4 spouse difficulty climbing one flight of stairs
gen s4clim1a =.
spouse r4clim1a, result(s4clim1a) wave(4)
label variable s4clim1a "s4clim1a:w4 S Some Diff-Clmb 1 flt str"
label values s4clim1a adldiff

***Difficulty stooping/keeling/crouching***
*wave 4 respondent difficulty stooping/keeling/crouching
gen r4stoopa =.
missing_common ph048d6, result(r4stoopa)		
replace r4stoopa = 0 if ph048d6==0
replace r4stoopa = 1 if ph048d6==1 
label variable r4stoopa "r4stoopa:w4 R Some Diff-Stoop/Kneel/Crch"
label values r4stoopa adldiff

*wave 4 spouse difficulty stooping/keeling/crouching
gen s4stoopa =.
spouse r4stoopa, result(s4stoopa) wave(4)
label variable s4stoopa "s4stoopa:w4 S Some Diff-Stoop/Kneel/Crch"
label values s4stoopa adldiff

***Difficulty lifting/carrying 10lbs***
*wave 4 respondent difficulty lifting/carrying 10lbs
gen r4lifta =.
missing_common ph048d9 , result(r4lifta)	
replace r4lifta = 0 if ph048d9==0
replace r4lifta = 1 if ph048d9==1
label variable r4lifta "r4lifta:w4 R Some Diff-Lift/carry 10lbs"
label values r4lifta adldiff

*wave 4 spouse difficulty lifting/carrying 10lbs
gen s4lifta =.
spouse r4lifta, result(s4lifta) wave(4)
label variable s4lifta "s4lifta:w4 S Some Diff-Lift/carry 10lbs"
label values s4lifta adldiff

***Difficulty picking up a small coin***
*wave 4 respondent difficulty picking up a small coin
gen r4dimea =.
missing_common ph048d10 , result(r4dimea)
replace r4dimea = 0 if ph048d10==0 
replace r4dimea = 1 if ph048d10==1 
label variable r4dimea "r4dimea:w4 R Some Diff-Pick up a small coin"
label values r4dimea adldiff

*wave 4 spouse difficulty picking up a small coin
gen s4dimea =.
spouse r4dimea, result(s4dimea) wave(4)
label variable s4dimea "s4dimea:w4 S Some Diff-Pick up a small coin"
label values s4dimea adldiff

***Difficulty reaching/extending arms up***
*wave 4 respondent difficulty reaching/extending arms up
gen r4armsa =.
missing_common ph048d7, result(r4armsa)
replace r4armsa = 0 if ph048d7==0
replace r4armsa = 1 if ph048d7==1
label variable r4armsa "r4armsa:w4 R Some Diff-Rch/xtnd arms up"
label values r4armsa adldiff

*wave 4 spouse difficulty reaching/extending arms up
gen s4armsa =.
spouse r4armsa, result(s4armsa) wave(4)
label variable s4armsa "s4armsa:w4 S Some Diff-Rch/xtnd arms up"
label values s4armsa adldiff

***Difficulty pushing/pulling a large object***
*wave 4 respondent difficulty pushing/pulling a large object
gen r4pusha =.
missing_common ph048d8, result(r4pusha)
replace r4pusha = 0 if ph048d8==0
replace r4pusha = 1 if ph048d8==1
label variable r4pusha "r4pusha:w4 R Some Diff-Push/pull lg obj"
label values r4pusha adldiff

*wave 4 spouse difficulty pushing/pulling a large object
gen s4pusha =.
spouse r4pusha, result(s4pusha) wave(4)
label variable s4pusha "s4pusha:w4 S Some Diff-Push/pull lg obj"
label values s4pusha adldiff

***ADL Summary 0-5***
*Wave 4 respondent ADL number of missings
egen r4adlam = rowmiss(r4batha r4dressa r4eata r4beda r4walkra) if inw4 == 1
label variable r4adlam "r4adlam:w4 Missings in ADL summary"

*wave 4 spouse ADL number of missings
gen s4adlam =.
spouse r4adlam, result(s4adlam) wave(4)
label variable s4adlam "s4adlam:w4 Missings in ADL summary"

local adl
foreach v of varlist r4batha r4dressa r4eata r4beda r4walkra {
	recode `v' (miss = 0), generate(`v'_m)
	local adl `adl' `v'_m
}

*Wave 4 respondent ADL Summary 0-5
gen r4adla =.
missing_h r4batha r4dressa r4eata r4beda r4walkra, result(r4adla)
replace r4adla = r4batha_m + r4dressa_m + r4eata_m + r4beda_m + r4walkra_m if inrange(r4adlam,0,4)
label variable r4adla "r4adla:w4 R Some Diff-ADLs /0-5"

*wave 4 spouse ADL Summary 0-5
gen s4adla =.
spouse r4adla, result(s4adla) wave(4)
label variable s4adla "s4adla:w4 S Some Diff-ADLs /0-5"

*drop intermediate variables
drop `adl'

***ADL Wallace Summary 0-3***
*Wave 4 respondent ADL Wallace number of missings
egen r4adlwam = rowmiss(r4batha r4dressa r4eata) if inw4 == 1
label variable r4adlwam "r4adlwam:w4 r Missings in ADL Wallace summary"

*wave 4 spouse ADL Wallace number of missings
gen s4adlwam =.
spouse r4adlwam, result(s4adlwam) wave(4)
label variable s4adlwam "s4adlwam:w4 s Missings in ADL Wallace summary"

local adlw
foreach v of varlist r4batha r4dressa r4eata {
	recode `v' (miss = 0), generate(`v'_m)
	local adlw `adlw' `v'_m
}

*Wave 4 respondent ADL Wallace Summary 0-3
gen r4adlwa =.
missing_h r4batha r4dressa r4eata, result(r4adlwa)
replace r4adlwa = r4batha_m + r4dressa_m + r4eata_m if inrange(r4adlwam,0,2)
label variable r4adlwa "r4adlwa:w4 R Some Diff-ADLs:Wallace /0-3"

*wave 4 spouse ADL Wallace Summary 0-3
gen s4adlwa =.
spouse r4adlwa, result(s4adlwa) wave(4)
label variable s4adlwa "s4adlwa:w4 S Some Diff-ADLs:Wallace /0-3"

*drop intermediate variables
drop `adlw'

***IADL Summary 0-3***
*Wave 4 respondent IADL number of missings
egen r4iadlam = rowmiss(r4phonea r4moneya r4medsa) if inw4 == 1
label variable r4iadlam "r4iadlam:w4 r Missings in IADL 0-3 summary"

*wave 4 spouse IADL number of missings
gen s4iadlam =.
spouse r4iadlam, result(s4iadlam) wave(4)
label variable s4iadlam "s4iadlam:w4 s Missings in IADL 0-3 summary"

local iadl
foreach v of varlist r4phonea r4moneya r4medsa {
	recode `v' (miss = 0), generate(`v'_m)
	local iadl `iadl' `v'_m
} 

*Wave 4 respondent IADLs Summary 0-3
gen r4iadla =.
missing_h r4phonea r4moneya r4medsa, result(r4iadla)
replace r4iadla = r4phonea_m + r4moneya_m + r4medsa_m if inrange(r4iadlam,0,2)
label variable r4iadla "r4iadla:w4 R Some Diff-IADLs: /0-3"

*wave 4 spouse IADLs Summary 0-3
gen s4iadla =.
spouse r4iadla, result(s4iadla) wave(4)
label variable s4iadla "s4iadla:w4 S Some Diff-IADLs: /0-3"

*drop intermediate variables
drop `iadl'

***IADL Summary 0-5***
*Wave 4 respondent IADL number of missings
egen r4iadlzam = rowmiss(r4phonea r4moneya r4medsa r4shopa r4mealsa) if inw4 == 1
label variable r4iadlzam "r4iadlzam:w4 r Missings in IADL 0-5 summary"

*wave 4 spouse IADL number of missings
gen s4iadlzam =.
spouse r4iadlzam, result(s4iadlzam) wave(4)
label variable s4iadlzam "s4iadlzam:w4 s Missings in IADL 0-5 summary"

local iadlz
foreach v of varlist r4phonea r4moneya r4medsa r4shopa r4mealsa {
	recode `v' (miss = 0), generate(`v'_m)
	local iadlz `iadlz' `v'_m
} 

*Wave 4 respondent IADLs Summary 0-5
gen r4iadlza =.
missing_h r4phonea r4moneya r4medsa r4shopa r4mealsa, result(r4iadlza)
replace r4iadlza = r4phonea_m + r4moneya_m + r4medsa_m + r4shopa_m + r4mealsa_m if inrange(r4iadlzam,0,4)
label variable r4iadlza "r4iadlza:w4 R Some Diff-IADLs: /0-5"

*wave 4 spouse IADLs Summary 0-5
gen s4iadlza =.
spouse r4iadlza, result(s4iadlza) wave(4)
label variable s4iadlza "s4iadlza:w4 S Some Diff-IADLs: /0-5"

*drop intermediate variables
drop `iadlz'

***Mobility Summary***
*Wave 4 respondent Mobility Summary number of missings
egen r4mobilbm = rowmiss(r4walk100a r4walkra r4climsa r4clim1a) if inw4 == 1
label variable r4mobilbm "r4mobilbm:w4 r Missings in mobility summary"

*wave 4 spouse Mobility Summary number of missings
gen s4mobilbm =.
spouse r4mobilbm, result(s4mobilbm) wave(4)
label variable s4mobilbm "s4mobilbm:w4 s Missings in mobility summary"

local mobilb
foreach v of varlist r4walk100a r4walkra r4climsa r4clim1a {
	recode `v' (miss = 0), generate(`v'_m)
	local mobilb `mobilb' `v'_m
} 

*Wave 4 respondent Mobility Summary
gen r4mobilb =.
missing_h r4walk100a r4walkra r4climsa r4clim1a, result(r4mobilb)
replace r4mobilb = r4walk100a_m + r4walkra_m + r4climsa_m + r4clim1a_m if inrange(r4mobilbm,0,3)
label variable r4mobilb "r4mobilb:w4 R Some Diff-Mobility /0-4"

*wave 4 spouse Mobility Summary
gen s4mobilb =.
spouse r4mobilb, result(s4mobilb) wave(4)
label variable s4mobilb "s4mobilb:w4 S Some Diff-Mobility /0-4"

*drop intermediate variables
drop `mobilb'

***Large Muscle Summary***
*Wave 4 respondent Large Muscle Summary number of missings
egen r4lgmusam = rowmiss(r4sita r4chaira r4stoopa r4pusha) if inw4 == 1
label variable r4lgmusam "r4lgmusam:w4 r Missings in large muscle summary"

*wave 4 spouse Large Muscle Summary number of missings
gen s4lgmusam =.
spouse r4lgmusam, result(s4lgmusam) wave(4)
label variable s4lgmusam "s4lgmusam:w4 s Missings in large muscle summary"

local lgmusa
foreach v of varlist r4sita r4chaira r4stoopa r4pusha {
	recode `v' (miss = 0), generate(`v'_m)
	local lgmusa `lgmusa' `v'_m
} 

*Wave 4 respondent Large Muscle Summary
gen r4lgmusa =.
missing_h r4sita r4chaira r4stoopa r4pusha, result(r4lgmusa)
replace r4lgmusa = r4sita_m + r4chaira_m + r4stoopa_m + r4pusha_m if inrange(r4lgmusam,0,3)
label variable r4lgmusa "r4lgmusa:w4 R Some Diff-Large muscle /0-4"

*wave 4 spouse Large Muscle Summary
gen s4lgmusa =.
spouse r4lgmusa, result(s4lgmusa) wave(4)
label variable s4lgmusa "s4lgmusa:w4 S Some Diff-Large muscle /0-4"

*drop intermediate variables
drop `lgmusa'

***Gross Motor Summary***
*Wave 4 respondent Gross Motor Summary number of missings
egen r4grossam = rowmiss(r4walk100a r4walkra r4clim1a r4beda r4batha) if inw4 == 1
label variable r4grossam "r4grossam:w4 r Missings in wk,rn,clmb,bd,bth summary"

*wave 4 spouse Gross Motor Summary number of missings
gen s4grossam =.
spouse r4grossam, result(s4grossam) wave(4)
label variable s4grossam "s4grossam:w4 s Missings in wk,rn,clmb,bd,bth summary"

local grossa
foreach v of varlist r4walk100a r4walkra r4clim1a r4beda r4batha {
	recode `v' (miss = 0), generate(`v'_m)
	local grossa `grossa' `v'_m
}

*Wave 4 respondent Gross Motor Summary
gen r4grossa =.
missing_h r4walk100a r4walkra r4clim1a r4beda r4batha, result(r4grossa)
replace r4grossa = r4walk100a_m + r4walkra_m + r4clim1a_m + r4beda_m + r4batha_m if inrange(r4grossam,0,4)
label variable r4grossa "r4grossa:w4 R Some Diff-Wk,rn,clmb,bd,bth /0-5"

*wave 4 spouse Gross Motor Summary
gen s4grossa =.
spouse r4grossa, result(s4grossa) wave(4)
label variable s4grossa "s4grossa:w4 S Some Diff-Wk,rn,clmb,bd,bth /0-5"

*drop intermediate variables
drop `grossa'

***Fine Motor Summary***
*Wave 4 respondent Fine Motor Summary number of missings
egen r4fineam = rowmiss(r4dimea r4eata r4dressa) if inw4 == 1
label variable r4fineam "r4fineam:w4 r Missings in dime,eat,dress summary"

*wave 4 spouse Gross Fine Summary number of missings
gen s4fineam =.
spouse r4fineam, result(s4fineam) wave(4)
label variable s4fineam "s4fineam:w4 s Missings in dime,eat,dress summary"

local finea
foreach v of varlist r4dimea r4eata r4dressa {
	recode `v' (miss = 0), generate(`v'_m)
	local finea `finea' `v'_m
}

*Wave 4 respondent Fine Motor Summary
gen r4finea =.
missing_h r4dimea r4eata r4dressa, result(r4finea)
replace r4finea = r4dimea_m + r4eata_m + r4dressa_m if inrange(r4fineam,0,2)
label variable r4finea "r4finea:w4 R Some Diff-dime,eat,dress /0-3"

*wave 4 spouse Fine Motor Summary
gen s4finea =.
spouse r4finea, result(s4finea) wave(4)
label variable s4finea "s4finea:w4 S Some Diff-dime,eat,dress /0-3"

*drop intermediate variables
drop `finea'

***EURO-D: Depression***
*wave 4 respondent depression
gen r4depress =.
missing_common euro1, result(r4depress)
replace r4depress = euro1 if inlist(euro1,0,1)
label variable r4depress "r4depress:w4 EURO-D: r Depression (0,1)"
label values r4depress heurod

*wave 4 spouse depression
gen s4depress =.
spouse r4depress, result(s4depress) wave(4)
label variable s4depress "s4depress:w4 EURO-D: s Depression (0,1)"
label values s4depress heurod

***EURO-D: Pessimism***
*wave 4 respondent pessimism
gen r4pessim =.
missing_common euro2, result(r4pessim)
replace r4pessim = euro2 if inlist(euro2,0,1)
label variable r4pessim "r4pessim:w4 EURO-D: r Pessimism (0,1)"
label values r4pessim heurod

*wave 4 spouse pessimism
gen s4pessim =.
spouse r4pessim, result(s4pessim) wave(4)
label variable s4pessim "s4pessim:w4 EURO-D: s Pessimism (0,1)"
label values s4pessim heurod

***EURO-D: Suicidality***
*wave 4 respondent suicidality
gen r4suicid =.
missing_common euro3, result(r4suicid)
replace r4suicid = euro3 if inlist(euro3,0,1)
label variable r4suicid "r4suicid:w4 EURO-D: r Suicidality (0,1)"
label values r4suicid heurod

*wave 4 spouse suicidality
gen s4suicid =.
spouse r4suicid, result(s4suicid) wave(4)
label variable s4suicid "s4suicid:w4 EURO-D: s Suicidality (0,1)"
label values s4suicid heurod

***EURO-D: Guilt***
*wave 4 respondent guilt
gen r4guilt =.
missing_common euro4, result(r4guilt)
replace r4guilt = euro4 if inlist(euro4,0,1)
label variable r4guilt "r4guilt:w4 EURO-D: r Guilt (0,1)"
label values r4guilt heurod

*wave 4 spouse guilt
gen s4guilt =.
spouse r4guilt, result(s4guilt) wave(4)
label variable s4guilt "s4guilt:w4 EURO-D: s Guilt (0,1)"
label values s4guilt heurod

***EURO-D: Sleep***
*wave 4 respondent sleep
gen r4sleep =.
missing_common euro5, result(r4sleep)
replace r4sleep = euro5 if inlist(euro5,0,1)
label variable r4sleep "r4sleep:w4 EURO-D: r Sleep (0,1)"
label values r4sleep heurod

*wave 4 spouse sleep
gen s4sleep =.
spouse r4sleep, result(s4sleep) wave(4)
label variable s4sleep "s4sleep:w4 EURO-D: s Sleep (0,1)"
label values s4sleep heurod

***EURO-D: Interest***
*wave 4 respondent interest
gen r4intrst =.
missing_common euro6, result(r4intrst)
replace r4intrst = euro6 if inlist(euro6,0,1)
label variable r4intrst "r4intrst:w4 EURO-D: r Interest (0,1)"
label values r4intrst heurod

*wave 4 spouse interest
gen s4intrst =.
spouse r4intrst, result(s4intrst) wave(4)
label variable s4intrst "s4intrst:w4 EURO-D: s Interest (0,1)"
label values s4intrst heurod

***EURO-D: Irritability***
*wave 4 respondent irritability
gen r4irritb =.
missing_common euro7, result(r4irritb)
replace r4irritb = euro7 if inlist(euro7,0,1)
label variable r4irritb "r4irritb:w4 EURO-D: r Irritability (0,1)"
label values r4irritb heurod

*wave 4 spouse irritability
gen s4irritb =.
spouse r4irritb, result(s4irritb) wave(4)
label variable s4irritb "s4irritb:w4 EURO-D: s Irritability (0,1)"
label values s4irritb heurod

***EURO-D: Appetite***
*wave 4 respondent appetite
gen r4appett =.
missing_common euro8, result(r4appett)
replace r4appett = euro8 if inlist(euro8,0,1)
label variable r4appett "r4appett:w4 EURO-D: r Appetite (0,1)"
label values r4appett heurod

*wave 4 spouse appetite
gen s4appett =.
spouse r4appett, result(s4appett) wave(4)
label variable s4appett "s4appett:w4 EURO-D: s Appetite (0,1)"
label values s4appett heurod

***EURO-D: Fatigue***
*wave 4 respondent fatigue
gen r4fatig =.
missing_common euro9, result(r4fatig)
replace r4fatig = euro9 if inlist(euro9,0,1)
label variable r4fatig "r4fatig:w4 EURO-D: r Fatigue (0,1)"
label values r4fatig heurod

*wave 4 spouse fatigue
gen s4fatig =.
spouse r4fatig, result(s4fatig) wave(4)
label variable s4fatig "s4fatig:w4 EURO-D: s Fatigue (0,1)"
label values s4fatig heurod

***EURO-D: Concentration***
*wave 4 respondent concentration
gen r4concnt =.
missing_common euro10, result(r4concnt)
replace r4concnt = euro10 if inlist(euro10,0,1)
label variable r4concnt "r4concnt:w4 EURO-D: r Concentration (0,1)"
label values r4concnt heurod

*wave 4 spouse concentration
gen s4concnt =.
spouse r4concnt, result(s4concnt) wave(4)
label variable s4concnt "s4concnt:w4 EURO-D: s Concentration (0,1)"
label values s4concnt heurod

***EURO-D: Enjoyment***
*wave 4 respondent enjoyment
gen r4enjoym =.
missing_common euro11, result(r4enjoym)
replace r4enjoym = euro11 if inlist(euro11,0,1)
label variable r4enjoym "r4enjoym:w4 EURO-D: r Enjoyment (0,1)"
label values r4enjoym heurod

*wave 4 spouse enjoyment
gen s4enjoym =.
spouse r4enjoym, result(s4enjoym) wave(4)
label variable s4enjoym "s4enjoym:w4 EURO-D: s Enjoyment (0,1)"
label values s4enjoym heurod

***EURO-D: Tearfulness***
*wave 4 respondent tearfulness
gen r4tearfl =.
missing_common euro12, result(r4tearfl)
replace r4tearfl = euro12 if inlist(euro12,0,1)
label variable r4tearfl "r4tearfl:w4 EURO-D: r Tearfulness (0,1)"
label values r4tearfl heurod

*wave 4 spouse tearfulness
gen s4tearfl =.
spouse r4tearfl, result(s4tearfl) wave(4)
label variable s4tearfl "s4tearfl:w4 EURO-D: s Tearfulness (0,1)"
label values s4tearfl heurod

***EURO-D: Score***
*wave 4 respondent EURO-D score
gen r4eurod =.
replace r4eurod = eurod
label variable r4eurod "r4eurod:w4 EURO-D: r Score"

*wave 4 respondent flag for EURO-D score
w4_imputation_flag eurod_f, gen(r4feurod)
label variable r4feurod "r4feurod:w4 EURO-D Flag: r Score"
label values r4feurod healthimpflag

*wave 4 spouse EURO-D score
gen s4eurod =.
spouse r4eurod, result(s4eurod) wave(4)
label variable s4eurod "s4eurod:w4 EURO-D: s Score"

*wave 4 spouse flag for EURO-D score
gen s4feurod =.
spouse r4feurod, result(s4feurod) wave(4)
label variable s4feurod "s4feurod:w4 EURO-D Flag: s Score"
label values s4feurod healthimpflag

***Ever had high blood pressure***
*wave 4 respondent ever had high blood pressure
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'hibpe == 1
}

gen r`wv'hibpe =.
missing_common ph006d2, result(r`wv'hibpe)
replace r`wv'hibpe = 0 if ph006d2 == 0
replace r`wv'hibpe = 1 if (ph006d2 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'hibpe "r`wv'hibpe:w`wv' r ever had high blood pressure"
label values r`wv'hibpe health_problems

*wave 4 spouse ever had high blood pressure
gen s`wv'hibpe =.
spouse r`wv'hibpe, result(s`wv'hibpe) wave(`wv')
label variable s`wv'hibpe "s`wv'hibpe:w`wv' s ever had high blood pressure"
label values s`wv'hibpe health_problems

***Ever had diabetes***
*wave 4 respondent ever had diabetes
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'diabe == 1
}

gen r`wv'diabe =.
missing_common ph006d5, result(r`wv'diabe)
replace r`wv'diabe = 0 if ph006d5 == 0 
replace r`wv'diabe = 1 if (ph006d5 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'diabe "r`wv'diabe:w`wv' r ever had diabetes"
label values r`wv'diabe health_problems

*wave 4 spouse ever had diabetes
gen s`wv'diabe =.
spouse r`wv'diabe, result(s`wv'diabe) wave(`wv')
label variable s`wv'diabe "s`wv'diabe:w`wv' s ever had diabetes"
label values s`wv'diabe health_problems

***Ever had cancer***
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'cancre == 1
}

gen preload = .
foreach w in `wvs_before' {
    replace preload = r`w'cancre if inw`w' == 1 & preload == .
}

*wave 4 respondent ever had cancer
gen r`wv'cancre =.
missing_common ph006d10, result(r`wv'cancre)
replace r`wv'cancre = 0 if ph006d10 == 0
replace r`wv'cancre = 1 if (ph006d10 == 1 | ph072_3 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'cancre "r`wv'cancre:w`wv' r ever had cancer"
label values r`wv'cancre health_problems

*wave 4 respondent cancer dispute flag
gen r`wv'cancrf = .
replace r`wv'cancrf = 0 if (inlist(ph006d10,0,1) | ph072_3 == 1 `or_reported_earlier') & inw`wv' == 1
replace r`wv'cancrf = 2 if ph073_3 == 5 & preload == 1
replace r`wv'cancrf = 3 if ph073_3 == 5 & preload == 0
label variable r`wv'cancrf "r`wv'cancrf:w`wv' r cancer dispute flag"
label values r`wv'cancrf ehealth_problems_flag

*update previous dispute flag
foreach w in `wvs_before' {
    replace r`w'cancrf = 5 if ph073_3 == 5 & r`w'cancre == 1 & r`w'cancrf == 0
    replace r`w'cancrf = 6 if ph073_3 == 5 & r`w'cancre == 0 & r`w'cancrf == 0
}

*update previous ever have varibles
foreach w in `wvs_before' {
    replace r`w'cancre = 0 if ph073_3 == 5 & r`w'cancre == 1
}

drop preload

***Ever had lung disease***
*wave 4 respondent ever had lung disease
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'lunge == 1
}

gen r`wv'lunge =.
missing_common ph006d6, result(r`wv'lunge)
replace r`wv'lunge = 0 if ph006d6 == 0 
replace r`wv'lunge = 1 if (ph006d6 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'lunge "r`wv'lunge:w`wv' r ever had lung disease"
label values r`wv'lunge health_problems

*wave 4 spouse ever had lung disease
gen s`wv'lunge =.
spouse r`wv'lunge, result(s`wv'lunge) wave(`wv')
label variable s`wv'lunge "s`wv'lunge:w`wv' s ever had lung disease"
label values s`wv'lunge health_problems

***Ever had heart problems***
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'hearte == 1
}

gen preload = .
foreach w in `wvs_before' {
    replace preload = r`w'hearte if inw`w' == 1 & preload == .
}

*wave 4 respondent ever had heart problems
gen r`wv'hearte =.
missing_common ph006d1, result(r`wv'hearte)
replace r`wv'hearte = 0 if ph006d1 == 0
replace r`wv'hearte = 1 if (ph006d1 == 1  | ph072_1 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'hearte "r`wv'hearte:w`wv' r ever had heart problems"
label values r`wv'hearte health_problems

*wave 4 respondent heart problems dispute flag
gen r`wv'heartf = .
replace r`wv'heartf = 0 if (inlist(ph006d1,0,1) | ph072_1 == 1 `or_reported_earlier') & inw`wv' == 1
replace r`wv'heartf = 2 if ph073_1 == 5 & preload == 1
replace r`wv'heartf = 3 if ph073_1 == 5 & preload == 0
label variable r`wv'heartf "r`wv'heartf:w`wv' r heart problems dispute flag"
label values r`wv'heartf ehealth_problems_flag

*update previous dispute flag
foreach w in `wvs_before' {
    replace r`w'heartf = 5 if ph073_1 == 5 & r`w'hearte == 1 & r`w'heartf == 0
    replace r`w'heartf = 6 if ph073_1 == 5 & r`w'hearte == 0 & r`w'heartf == 0
}

*update previous ever have varibles
foreach w in `wvs_before' {
    replace r`w'hearte = 0 if ph073_1 == 5 & r`w'hearte == 1
}

drop preload

***Ever had stroke***
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'stroke == 1
}

gen preload = .
foreach w in `wvs_before' {
    replace preload = r`w'stroke if inw`w' == 1 & preload == .
}

*wave 4 respondent ever had stroke
gen r`wv'stroke =.
missing_common ph006d4 , result(r`wv'stroke)
replace r`wv'stroke = 0 if ph006d4 == 0
replace r`wv'stroke = 1 if (ph006d4 == 1 | ph072_2 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'stroke "r`wv'stroke:w`wv' r ever had stroke"
label values r`wv'stroke health_problems

*wave 4 respondent stroke dispute flag
gen r`wv'strokf = .
replace r`wv'strokf = 0 if (inlist(ph006d4,0,1) | ph072_2 == 1 `or_reported_earlier') & inw`wv' == 1
replace r`wv'strokf = 2 if ph073_2 == 5 & preload == 1
replace r`wv'strokf = 3 if ph073_2 == 5 & preload == 0
label variable r`wv'strokf "r`wv'strokf:w`wv' r stroke dispute flag"
label values r`wv'strokf ehealth_problems_flag

*update previous dispute flag
foreach w in `wvs_before' {
    replace r`w'strokf = 5 if ph073_2 == 5 & r`w'stroke == 1 & r`w'strokf == 0
    replace r`w'strokf = 6 if ph073_2 == 5 & r`w'stroke == 0 & r`w'strokf == 0
}

*update previous ever have varibles
foreach w in `wvs_before' {
    replace r`w'stroke = 0 if ph073_2 == 5 & r`w'stroke == 1
}

drop preload

***Ever had arthritis***
*wave 4 respondent ever had arthritis
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'arthre == 1
}

gen r`wv'arthre =.
missing_common ph006d8 , result(r`wv'arthre)
replace r`wv'arthre = 0 if ph006d8 == 0
replace r`wv'arthre = 1 if (ph006d8 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'arthre "r`wv'arthre:w`wv' r ever had arthritis"
label values r`wv'arthre health_problems

*wave 4 spouse ever had arthritis
gen s`wv'arthre =.
spouse r`wv'arthre, result(s`wv'arthre) wave(`wv')
label variable s`wv'arthre "s`wv'arthre:w`wv' s ever had arthritis"
label values s`wv'arthre health_problems

***Ever had high cholesterol***
*wave 4 respondent ever had high cholesterol
local or_reported_earlier
foreach w in `wvs_before' {
	local or_reported_earlier `or_reported_earlier' | r`w'hchole == 1
}

gen r`wv'hchole = .
missing_common ph006d3, result(r`wv'hchole)
replace r`wv'hchole = 0 if ph006d3 == 0
replace r`wv'hchole = 1 if (ph006d3 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'hchole "r`wv'hchole:w`wv' r ever had high cholesterol"
label values r`wv'hchole health_problems

*wave 4 spouse ever had high cholesterol
gen s`wv'hchole = .
spouse r`wv'hchole, result(s`wv'hchole) wave(`wv')
label variable s`wv'hchole "s`wv'hchole:w`wv' s ever had high cholesterol"
label values s`wv'hchole health_problems

***Ever had Parkinsons***
*wave 4 respondent ever had parkinsons
local or_reported_earlier
foreach w in `wvs_before' {
	local or_reported_earlier `or_reported_earlier' | r`w'parkine == 1
}

gen r`wv'parkine = .
missing_common ph006d12, result(r`wv'parkine)
replace r`wv'parkine = 0 if ph006d12 == 0
replace r`wv'parkine = 1 if (ph006d12 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'parkine "r`wv'parkine:w`wv' r ever had parkinson disease"
label values r`wv'parkine health_problems

*wave 4 spouse ever had parkinsons
gen s`wv'parkine = .
spouse r`wv'parkine, result(s`wv'parkine) wave(`wv')
label variable s`wv'parkine "s`wv'parkine:w`wv' s ever had parkinson disease"
label values s`wv'parkine health_problems

***Ever had cataracts***
*wave 4 respondent ever had cataracts
local or_reported_earlier
foreach w in `wvs_before' {
	local or_reported_earlier `or_reported_earlier' | r`w'catrcte == 1
}

gen r`wv'catrcte = .
missing_common ph006d13, result(r`wv'catrcte)
replace r`wv'catrcte = 0 if ph006d13 == 0
replace r`wv'catrcte = 1 if (ph006d13 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'catrcte "r`wv'catrcte:w`wv' r ever had cataracts"
label values r`wv'catrcte health_problems

*wave 4 spouse ever had cataracts
gen s`wv'catrcte = .
spouse r`wv'catrcte, result(s`wv'catrcte) wave(`wv')
label variable s`wv'catrcte "s`wv'catrcte:w`wv' s ever had cataracts"
label values s`wv'catrcte health_problems

***Ever had hip or femoral fracture***
local or_reported_earlier
foreach w in `wvs_before' {
	local or_reported_earlier `or_reported_earlier' | r`w'hipfeme == 1
}

gen preload = .
foreach w in `wvs_before' {
	replace preload = r`w'hipfeme if inw`w' == 1 & preload == .
}

*wave 4 respondent ever had hip or femoral fracture
gen r`wv'hipfeme = .
missing_common ph006d14, result(r`wv'hipfeme)
replace r`wv'hipfeme = 0 if ph006d14 == 0
replace r`wv'hipfeme = 1 if (ph006d14 == 1 | ph072_4 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'hipfeme "r`wv'hipfeme:w`wv' r ever had hip/femoral fracture"
label values r`wv'hipfeme health_problems

*wave 4 respondent hip/femoral fracture dispute flag
gen r`wv'hipfemf = .
replace r`wv'hipfemf = 0 if (inlist(ph006d14,0,1) | ph072_4 == 1 `or_reported_earlier') & inw`wv' == 1
replace r`wv'hipfemf = 2 if ph073_4 == 5 & preload == 1
replace r`wv'hipfemf = 3 if ph073_4 == 5 & preload == 0
label variable r`wv'hipfemf "r`wv'hipfemf:w`wv' r hip/femoral fracture dispute flag"
label values r`wv'hipfemf ehealth_problems_flag

*update previous dispute flag
foreach w in `wvs_before' {
	replace r`w'hipfemf = 5 if ph073_4 == 5 & r`w'hipfeme == 1 & r`w'hipfemf == 0
	replace r`w'hipfemf = 6 if ph073_4 == 5 & r`w'hipfeme == 0 & r`w'hipfemf == 0
}

*update previous ever have variables
foreach w in `wvs_before' {
	replace r`w'hipfeme = 0 if ph073_4 == 5 & r`w'hipfeme == 1
}

drop preload

***Ever had psychiatric disorder***
*wave 4 respondent ever had psych disorder
gen r`wv'psyche = .
missing_common mh022, result(r`wv'psyche)
replace r`wv'psyche = 0 if mh022 == 5
replace r`wv'psyche = 1 if mh022 == 1
label variable r`wv'psyche "r`wv'psyche:w`wv' r ever had psych disorder"
label values r`wv'psyche health_problems

*wave 4 spouse ever had psych disorder
gen s`wv'psyche = .
spouse r`wv'psyche, result(s`wv'psyche) wave(`wv')
label variable s`wv'psyche "s`wv'psyche:w`wv' s ever had psych disorder"
label values s`wv'psyche health_problems

***Ever had ulcer***
*wave 4 respondent ever had ulcer
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'ulcer == 1
}

gen r`wv'ulcer =.
missing_common ph006d11, result(r`wv'ulcer)
replace r`wv'ulcer = 0 if ph006d11 == 0
replace r`wv'ulcer = 1 if (ph006d11 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'ulcer "r`wv'ulcer:w`wv' r ever had ulcer"
label values r`wv'ulcer health_problems

*wave 4 spouse ever had ulcer
gen s`wv'ulcer =.
spouse r`wv'ulcer, result(s`wv'ulcer) wave(`wv')
label variable s`wv'ulcer "s`wv'ulcer:w`wv' s ever had ulcer"
label values s`wv'ulcer health_problems

***Ever had Alzheimerís disease, dementia***
*wave 4 respondent ever had Alzheimerís disease, dementia
local or_reported_earlier
foreach w in 2 {
    local or_reported_earlier `or_reported_earlier' | r`w'alzdem == 1
}

gen r`wv'alzdem =.
missing_common ph006d16 , result(r`wv'alzdem)
replace r`wv'alzdem = 0 if ph006d16 == 0
replace r`wv'alzdem = 1 if (ph006d16 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'alzdem "r`wv'alzdem:w`wv' r ever had alzheimer's/dementia"
label values r`wv'alzdem health_problems

*wave 4 spouse ever had Alzheimerís disease, dementia
gen s`wv'alzdem =.
spouse r`wv'alzdem, result(s`wv'alzdem) wave(`wv')
label variable s`wv'alzdem "s`wv'alzdem:w`wv' s ever had alzheimerís/dementia"
label values s`wv'alzdem health_problems

***Height in meters***
*wave 4 respondent height in meters
gen r4height =.
missing_common ph013_, result(r4height)
replace r4height = r1height if !mi(r1height) & inw4==1
replace r4height = r2height if !mi(r2height) & inw4==1
replace r4height = .i if inrange(ph013_,0,89) 
replace r4height = ph013_ if inrange(ph013_,1.00,1.90) & inw4==1
replace r4height = ph013_/100 if inrange(ph013_,90,220) & inw4==1 
label variable r4height "r4height:w4 r Height in meters"

*wave 4 spouse height in meters
gen s4height =.
spouse r4height, result(s4height) wave(4)
label variable s4height "s4height:w4 s Height in meters"

***Weight in kilograms***
*wave 4 respondent weight in kilograms
gen r4weight =.
missing_common ph012_, result(r4weight)
replace r4weight = .i if inrange(ph012_,0,12) 
replace r4weight = ph012_ if inrange(ph012_,13,250) 
label variable r4weight "r4weight:w4 r Weight in kilograms"

*wave 4 spouse weight in kilograms
gen s4weight =.
spouse r4weight, result(s4weight) wave(4)
label variable s4weight "s4weight:w4 s Weight in kilograms"

***BMI***
*wave 4 respondent BMI
gen r4bmi =.
missing_h r4weight r4height, result(r4bmi)
replace r4bmi = r4weight/(r4height^2) if !mi(r4weight) & !mi(r4height)
replace r4bmi = .i if (r4weight==.i | r4height==.i | !inrange(r4bmi,12,100)) & inw`wv' == 1
label variable r4bmi "r4bmi:w4 r Body Mass Index=kg/m2"

*wave 4 spouse BMI
gen s4bmi =.
spouse r4bmi, result(s4bmi) wave(4)
label variable s4bmi "s4bmi:w4 s Body Mass Index=kg/m2"

***BMI Categorization***
*wave 4 respondent BMI categorization
gen r4bmicat = .
missing_h r4weight r4height, result(r4bmicat)
replace r4bmicat = 1 if inrange(r4bmi,1,18.49999)
replace r4bmicat = 2 if inrange(r4bmi,18.50,24.99999)
replace r4bmicat = 3 if inrange(r4bmi,25,29.99999)
replace r4bmicat = 4 if inrange(r4bmi,30,34.99999)
replace r4bmicat = 5 if inrange(r4bmi,35,39.99999)
replace r4bmicat = 6 if inrange(r4bmi,40,100)
replace r4bmicat = .i if r4weight==.i | r4height==.i | r4bmi==.i
label variable r4bmicat "r4bmicat:w4 r BMI categorization"
label values r4bmicat bmicate

*wave 4 spouse BMI categorization
gen s4bmicat = .
spouse r4bmicat, result(s4bmicat) wave(4)
label variable s4bmicat "s4bmicat:w4 s BMI categorization"
label values s4bmicat bmicate

***Vigorous physical activity***
*wave 4 respondent vigorous physical activity
gen r4vgactx =.
missing_common br015_, result(r4vgactx)
replace r4vgactx = 2 if br015_ == 1
replace r4vgactx = 3 if br015_ == 2
replace r4vgactx = 4 if br015_ == 3
replace r4vgactx = 5 if br015_ == 4
label variable r4vgactx "r4vgactx:w4 R Freq vigorous phys activ"
label values r4vgactx activity

*wave 4 spouse vigorous physical activity
gen s4vgactx =.
spouse r4vgactx, result(s4vgactx) wave(4)
label variable s4vgactx "s4vgactx:w4 S Freq vigorous phys activ"
label values s4vgactx activity

***Moderate physical activity***
*wave 4 respondent moderate physical activity
gen r4mdactx =.
missing_common br016_, result(r4mdactx)
replace r4mdactx = 2 if br016_ == 1
replace r4mdactx = 3 if br016_ == 2
replace r4mdactx = 4 if br016_ == 3
replace r4mdactx = 5 if br016_ == 4
label variable r4mdactx "r4mdactx:w4 R Freq moderate phys activ"
label values r4mdactx activity

*wave 4 spouse vigorous moderate activity
gen s4mdactx =.
spouse r4mdactx, result(s4mdactx) wave(4)
label variable s4mdactx "s4mdactx:w4 S Freq moderate phys activ"
label values s4mdactx activity

***Ever drinks alcohol***
**change to last 7 days
*wave 4 respondent ever drinks alcohol
gen r4drink =.
missing_common br010_, result(r4drink)
replace r4drink = 0 if inlist(br010_,5,6,7)
replace r4drink = 1 if inlist(br010,1,2,3,4)
label variable r4drink "r4drink:w4 R ever drinks any alcohol"
label values r4drink ever_drink

*wave 4 spouse ever drinks alcohol
gen s4drink =.
spouse r4drink, result(s4drink) wave(4)
label variable s4drink "s4drink:w4 S ever drinks any alcohol"
label values s4drink ever_drink

***Days per week drinks***
*wave 4 respondent days per week drinks
gen r4drinkx =.
missing_common br010_, result(r4drinkx)
replace r4drinkx = 0 if inlist(br010_,6,7)
replace r4drinkx = 1 if br010_ == 5
replace r4drinkx = 2 if inlist(br010_,4,3) 
replace r4drinkx = 3 if br010_ == 2 
replace r4drinkx = 4 if br010_ == 1 
label variable r4drinkx "r4drinkx:w4 R Frequency of drinking"
label values r4drinkx drinkx

*wave 4 spouse days per week drinks
gen s4drinkx =.
spouse r4drinkx, result(s4drinkx) wave(4)
label variable s4drinkx "s4drinkx:w4 S Frequency of drinking"
label values s4drinkx drinkx

***No. Drinks per day when drinks***
*wave 4 respondent no. drinks per day when drinks
gen r4drinkn = .
missing_common br019_, result(r4drinkn)
replace r4drinkn = br019_ if inrange(br019_,0,70)
replace r4drinkn = 0 if br010_ == 7
label variable r4drinkn "r4drinkn:w4 r no. drinks per day"

*wave 4 spouse no. drinks per day when drinks
gen s4drinkn = .
spouse r4drinkn, result(s4drinkn) wave(4)
label variable s4drinkn "s4drinkn:w4 s no. drinks per day"

***Ever smoked cigarettes***
*wave 4 respondent ever smoked cigarettes
gen r4smokev =.
missing_common br001_, result(r4smokev)
replace r4smokev = r1smokev if !mi(r1smokev) & inw4 == 1
replace r4smokev = r2smokev if !mi(r2smokev) & inw4 == 1
replace r4smokev = 0 if br001_ == 5
replace r4smokev = 1 if br001_ == 1 | br002_ == 1
label variable r4smokev "r4smokev:w4 R smoke ever"
label values r4smokev smokes

*wave 4 spouse ever smoked cigarettes
gen s4smokev =.
spouse r4smokev, result(s4smokev) wave(4)
label variable s4smokev "s4smokev:w4 S smoke ever"
label values s4smokev smokes

***Smokes cigarettes now***
*wave 4 respondent smokes cigarettes now
***check br022_ 
gen r4smoken =.
missing_common br001_ br002_, result(r4smoken)
replace r4smoken = 0 if br001_ == 5 | (br002_ == 5 & br022_ != 3)
replace r4smoken = 1 if br002_ == 1 | br022_ == 3
label variable r4smoken "r4smoken:w4 R smokes now"
label values r4smoken smokes

*wave 4 spouse smokes cigarettes now
gen s4smoken =.
spouse r4smoken, result(s4smoken) wave(4)
label variable s4smoken "s4smoken:w4 S smokes now"
label values s4smoken smokes


***drop wave 4 Behavioural Risk file raw variables
drop `health_w4_br' 

***drop wave 4 Mental Health file raw variables
drop `health_w4_mh'

***drop wave 4 Physical Health file raw variables
drop `health_w4_ph'

***drop wave 4 Generated Variables (Health) file raw variables
drop `health_w4_gv_health'

***drop wave 4 financial derived file raw variables***
drop `health_w4_imput'

******************************************************************************************


*set wave number
local wv = 5
local pre_wv = `wv' - 1
local wvs_before 4 2 1

***merge with wave 5 Behavioural Risk data***
local health_w5_br br015_ br016_ br010_ br001_ br002_ br022_ br019_ 
merge 1:1 mergeid using "$wave_5_br", keepusing(`health_w5_br') nogen

***merge with wave 5 Physical Health data***
local health_w5_ph ph003_ ph005_ ph049d2 ph049d1 ph049d3 ///
					ph049d4 ph049d5 ph049d6 ph049d7 ph049d10 ph049d13 ph049d11 ph049d12 ///
					ph049d9 ph049d8 ph048d1 ph048d2 ph048d3 ph048d4 ph048d5 ph048d6 ///
					ph048d9 ph048d10 ph048d7 ph048d8 ph006d2 ph006d3 ph006d5 ph006d10 ///
					ph006d6 ph006d1 ph006d4 ph013_ ph012_ ph006d11 ph006d12 ph006d13 ph006d14 ///
					ph006d16 ph006d18 ph006d19 ph006d20 ph072_? ph073_?
merge 1:1 mergeid using "$wave_5_ph", keepusing(`health_w5_ph') nogen

***merge with wave 5 Generated Variables (Health) data***
local health_w5_gv_health euro1 euro2 euro3 euro4 euro5 euro6 euro7 ///
                          euro8 euro9 euro10 euro11 euro12
merge 1:1 mergeid using "$wave_5_gv_health", keepusing(`health_w5_gv_health') nogen

***merge with wave 5 imputation data***
local health_w5_imput eurod eurod_f
merge 1:1 mergeid using "$wave_5_imputations_", keepusing(`health_w5_imput') nogen


***self-report of health***
*wave 5 respondent self-report of health
gen r5shlt =.
missing_common ph003_ , result(r5shlt)
replace r5shlt = 1 if ph003_==1
replace r5shlt = 2 if ph003_==2
replace r5shlt = 3 if ph003_==3
replace r5shlt = 4 if ph003_==4 
replace r5shlt = 5 if ph003_==5 
label variable r5shlt "r5shlt:w5 r self-report of health"
label values r5shlt health

*wave 5 spouse self-report of health
gen s5shlt =.
spouse r5shlt, result(s5shlt) wave(5)
label variable s5shlt "s5shlt:w5 s self-report of health"
label values s5shlt health

***Whether health limits activities***
*wave 5 respondent whether health limits activites
gen r5hlthlma =.
missing_common ph005_, result(r5hlthlma)
replace r5hlthlma = 0 if ph005_ == 3
replace r5hlthlma = 1 if inrange(ph005_,1,2)
label variable r5hlthlma "r5hlthlma:w5 r hlth problem limits activities"
label values r5hlthlma limits

*wave 5 spouse whether health limits activities
gen s5hlthlma =.
spouse r5hlthlma, result(s5hlthlma) wave(5)
label variable s5hlthlma "s5hlthlma:w5 s hlth problem limits activities"
label values s5hlthlma limits

***Some difficulty walking cross room***
*wave 5 respondent difficulty walking across room
gen r5walkra =.
missing_common ph049d2, result(r5walkra)	
replace r5walkra = 0 if ph049d2==0 
replace r5walkra = 1 if ph049d2==1 
label variable r5walkra "r5walkra:w5 R Some Diff-Walk across room"
label values r5walkra adldiff

*wave 5 spouse difficulty walking across room
gen s5walkra =.
spouse r5walkra, result(s5walkra) wave(5)
label variable s5walkra "s5walkra:w5 S Some Diff-Walk across room"
label values s5walkra adldiff

***Some difficulty dressing***
*wave 5 respondent difficulty dressing
gen r5dressa =.
missing_common ph049d1 , result(r5dressa)		
replace r5dressa = 0 if ph049d1==0 
replace r5dressa = 1 if ph049d1==1 
label variable r5dressa "r5dressa:w5 R Some Diff-Dressing"
label values r5dressa adldiff

*wave 5 spouse difficulty dressing
gen s5dressa =.
spouse r5dressa, result(s5dressa) wave(5)
label variable s5dressa "s5dressa:w5 S Some Diff-Dressing"
label values s5dressa adldiff

***Some difficulty bathing, shower***
*wave 5 respondent difficulty bathing, shower
gen r5batha =.
missing_common ph049d3 , result(r5batha)		
replace r5batha = 0 if ph049d3==0 
replace r5batha = 1 if ph049d3==1 
label variable r5batha "r5batha:w5 R Some Diff-Bathing, shower"
label values r5batha adldiff

*wave 5 spouse difficulty bathing, shower
gen s5batha =.
spouse r5batha, result(s5batha) wave(5)
label variable s5batha "s5batha:w5 S Some Diff-Bathing, shower"
label values s5batha adldiff

***Some difficulty eating***
*wave 5 respondent difficulty eating
gen r5eata =.
missing_common ph049d4 , result(r5eata)
replace r5eata = 0 if ph049d4==0 
replace r5eata = 1 if ph049d4==1 
label variable r5eata "r5eata:w5 R Some Diff-Eating"
label values r5eata adldiff

*wave 5 spouse difficulty eating
gen s5eata =.
spouse r5eata, result(s5eata) wave(5)
label variable s5eata "s5eata:w5 S Some Diff-Eating"
label values s5eata adldiff

***Some difficulty getting in/out of bed***
*wave 5 respondent difficulty getting in/out of bed
gen r5beda =.
missing_common ph049d5 , result(r5beda)
replace r5beda = 0 if ph049d5==0  
replace r5beda = 1 if ph049d5==1 
label variable r5beda "r5beda:w5 R Some Diff-Get in/out bed"
label values r5beda adldiff

*wave 5 spouse difficulty getting in/out of bed
gen s5beda =.
spouse r5beda, result(s5beda) wave(5)
label variable s5beda "s5beda:w5 S Some Diff-Get in/out bed"
label values s5beda adldiff


***Some difficulty using the toilet***
*wave 5 respondent difficulty using the toilet
gen r5toilta =.
missing_common ph049d6 , result(r5toilta)			
replace r5toilta = 0 if ph049d6==0  
replace r5toilta = 1 if ph049d6==1 
label variable r5toilta "r5toilta:w5 R Some Diff-Using the toilet"
label values r5toilta adldiff

*wave 5 spouse difficulty using the toilet
gen s5toilta =.
spouse r5toilta, result(s5toilta) wave(5)
label variable s5toilta "s5toilta:w5 S Some Diff-Using the toilet"
label values s5toilta adldiff

***Some difficulty using using a map***
*wave 5 respondent difficulty using a map
gen r5mapa =.
missing_common ph049d7 , result(r5mapa)	
replace r5mapa = 0 if ph049d7==0 
replace r5mapa = 1 if ph049d7==1  
label variable r5mapa "r5mapa:w5 R Some Diff-Use a map"
label values r5mapa adldiff

*wave 5 spouse difficulty using a map
gen s5mapa =.
spouse r5mapa, result(s5mapa) wave(5)
label variable s5mapa "s5mapa:w5 S Some Diff-Use a map"
label values s5mapa adldiff

***Some difficulty using a telephone***
*wave 5 respondent difficulty using a telephone
gen r5phonea =.
missing_common ph049d10, result(r5phonea)			
replace r5phonea = 0 if ph049d10==0 
replace r5phonea = 1 if ph049d10==1 
label variable r5phonea "r5phonea:w5 R Some Diff-Use telephone"
label values r5phonea adldiff

*wave 5 spouse difficulty using a telephone
gen s5phonea =.
spouse r5phonea, result(s5phonea) wave(5)
label variable s5phonea "s5phonea:w5 S Some Diff-Use a telephone"
label values s5phonea adldiff

***Some difficulty managing money***
*wave 5 respondent difficulty managing money
gen r5moneya =.
missing_common ph049d13 , result(r5moneya)				
replace r5moneya = 0 if ph049d13==0 
replace r5moneya = 1 if ph049d13==1
label variable r5moneya "r5moneya:w5 R Some Diff-Managing money"
label values r5moneya adldiff

*wave 5 spouse difficulty managing money
gen s5moneya =.
spouse r5moneya, result(s5moneya) wave(5)
label variable s5moneya "s5moneya:w5 S Some Diff-Managing money"
label values s5moneya adldiff

***Some difficulty taking medications***
*wave 5 respondent difficulty taking medications
gen r5medsa =.
missing_common ph049d11 , result(r5medsa)					
replace r5medsa = 0 if ph049d11==0 
replace r5medsa = 1 if ph049d11==1 
label variable r5medsa "r5medsa:w5 R Some Diff-Take medications"
label values r5medsa adldiff

*wave 5 spouse difficulty taking medications
gen s5medsa =.
spouse r5medsa, result(s5medsa) wave(5)
label variable s5medsa "s5medsa:w5 S Some Diff-Take medications"
label values s5medsa adldiff

***Some difficulty shopping for groceries***
*wave 5 respondent difficulty shopping for groceries
gen r5shopa =.
missing_common ph049d9, result(r5shopa)			
replace r5shopa = 0 if ph049d9==0 
replace r5shopa = 1 if ph049d9==1 
label variable r5shopa "r5shopa:w5 R Some Diff-Shop for grocery"
label values r5shopa adldiff

*wave 1 spouse difficulty shopping for groceries
gen s5shopa =.
spouse r5shopa, result(s5shopa) wave(5)
label variable s5shopa "s5shopa:w5 S Some Diff-Shop for grocery"
label values s5shopa adldiff

***Some difficulty preparing a hot meal***
*wave 5 respondent difficulty preparing a hot meal
gen r5mealsa =.
missing_common ph049d8 , result(r5mealsa)
replace r5mealsa = 0 if ph049d8==0  
replace r5mealsa = 1 if ph049d8==1 
label variable r5mealsa "r5mealsa:w5 R Some Diff-Prepare hot meal"
label values r5mealsa adldiff

*wave 5 spouse difficulty preparing a hot meal
gen s5mealsa =.
spouse r5mealsa, result(s5mealsa) wave(5)
label variable s5mealsa "s5mealsa:w5 S Some Diff-Prepare hot meal"
label values s5mealsa adldiff

***Some difficulty doing work around the house***
*wave 5 respondent difficulty doing work around the house
gen r5housewka = .
missing_common ph049d12, result(r5housewka)
replace r5housewka = 0 if ph049d12==0
replace r5housewka = 1 if ph049d12==1
label variable r5housewka "r5housewka:w5 R some diff-doing work around house"
label values r5housewka adldiff

*wave 5 spouse difficulty doing work around the house
gen s5housewka = .
spouse r5housewka, result(s5housewka) wave(5)
label variable s5housewka "s5housewka:w5 s some diff-doing work around house"
label values s5housewka adldiff

***Difficulty Walking 100m***
*wave 5 respondent difficulty walking 100m
gen r5walk100a =.
missing_common ph048d1 , result(r5walk100a)
replace r5walk100a = 0 if ph048d1==0  
replace r5walk100a = 1 if ph048d1==1 
label variable r5walk100a "r5walk100a:w5 R Some Diff-Walk 100m"
label values r5walk100a adldiff

*wave 5 spouse difficulty walking 100m
gen s5walk100a =.
spouse r5walk100a, result(s5walk100a) wave(5)
label variable s5walk100a "s5walk100a:w5 S Some Diff-Walk 100m"
label values s5walk100a adldiff

***Difficulty sitting for 2 hours***
*wave 5 respondent difficulty sitting for 2 hours
gen r5sita =.
missing_common ph048d2 , result(r5sita)
replace r5sita = 0 if ph048d2==0
replace r5sita = 1 if ph048d2==1 
label variable r5sita "r5sita:w5 R Some Diff-Sit for 2 hours"
label values r5sita adldiff

*wave 1 spouse difficulty sitting for 2 hours
gen s5sita =.
spouse r5sita, result(s5sita) wave(5)
label variable s5sita "s5sita:w5 S Some Diff-Sit for 2 hours"
label values s5sita adldiff

***Difficulty getting up from a chair***
*wave 5 respondent difficulty getting up from a chair
gen r5chaira =.
missing_common ph048d3  , result(r5chaira)	
replace r5chaira = 0 if ph048d3==0 
replace r5chaira = 1 if ph048d3==1
label variable r5chaira "r5chaira:w5 R Some Diff-Get up fr chair"
label values r5chaira adldiff

*wave 5 spouse difficulty getting up from a chair
gen s5chaira =.
spouse r5chaira, result(s5chaira) wave(5)
label variable s5chaira "s5chaira:w5 S Some Diff-Get up fr chair"
label values s5chaira adldiff

***Difficulty climbing several flights of stairs***
*wave 5 respondent difficulty climbing several flights of stairs
gen r5climsa =.
missing_common ph048d4 , result(r5climsa)
replace r5climsa = 0 if ph048d4==0 
replace r5climsa = 1 if ph048d4==1 
label variable r5climsa "r5climsa:w5 R Some Diff-Clmb sev flt str"
label values r5climsa adldiff

*wave 5 spouse difficulty climbing several flights of stairs
gen s5climsa =.
spouse r5climsa, result(s5climsa) wave(5)
label variable s5climsa "s5climsa:w5 S Some Diff-Clmb sev flt str"
label values s5climsa adldiff

***Difficulty climbing one flight of stairs***
*wave 5 respondent difficulty climbing one flight of stairs
gen r5clim1a =.
missing_common ph048d5 , result(r5clim1a)		
replace r5clim1a = 0 if ph048d5==0 
replace r5clim1a = 1 if ph048d5==1
label variable r5clim1a "r5clim1a:w5 R Some Diff-Clmb 1 flt str"
label values r5clim1a adldiff

*wave 5 spouse difficulty climbing one flight of stairs
gen s5clim1a =.
spouse r5clim1a, result(s5clim1a) wave(5)
label variable s5clim1a "s5clim1a:w5 S Some Diff-Clmb 1 flt str"
label values s5clim1a adldiff

***Difficulty stooping/kneeling/crouching***
*wave 5 respondent difficulty stooping/kneeling/crouching
gen r5stoopa =.
missing_common ph048d6, result(r5stoopa)		
replace r5stoopa = 0 if ph048d6==0
replace r5stoopa = 1 if ph048d6==1 
label variable r5stoopa "r5stoopa:w5 R Some Diff-Stoop/Kneel/Crch"
label values r5stoopa adldiff

*wave 5 spouse difficulty stooping/kneeling/crouching
gen s5stoopa =.
spouse r5stoopa, result(s5stoopa) wave(5)
label variable s5stoopa "s5stoopa:w5 S Some Diff-Stoop/Kneel/Crch"
label values s5stoopa adldiff

***Difficulty lifting/carrying 10lbs***
*wave 5 respondent difficulty lifting/carrying 10lbs
gen r5lifta =.
missing_common ph048d9 , result(r5lifta)	
replace r5lifta = 0 if ph048d9==0
replace r5lifta = 1 if ph048d9==1
label variable r5lifta "r5lifta:w5 R Some Diff-Lift/carry 10lbs"
label values r5lifta adldiff

*wave 5 spouse difficulty lifting/carrying 10lbs
gen s5lifta =.
spouse r5lifta, result(s5lifta) wave(5)
label variable s5lifta "s5lifta:w5 S Some Diff-Lift/carry 10lbs"
label values s5lifta adldiff

***Difficulty picking up a small coin***
*wave 5 respondent difficulty picking up a small coin
gen r5dimea =.
missing_common ph048d10 , result(r5dimea)
replace r5dimea = 0 if ph048d10==0 
replace r5dimea = 1 if ph048d10==1 
label variable r5dimea "r5dimea:w5 R Some Diff-Pick up a small coin"
label values r5dimea adldiff

*wave 5 spouse difficulty picking up a small coin
gen s5dimea =.
spouse r5dimea, result(s5dimea) wave(5)
label variable s5dimea "s5dimea:w5 S Some Diff-Pick up a small coin"
label values s5dimea adldiff

***Difficulty reaching/extending arms up***
*wave 5 respondent difficulty reaching/extending arms up
gen r5armsa =.
missing_common ph048d7, result(r5armsa)
replace r5armsa = 0 if ph048d7==0
replace r5armsa = 1 if ph048d7==1
label variable r5armsa "r5armsa:w5 R Some Diff-Rch/xtnd arms up"
label values r5armsa adldiff

*wave 5 spouse difficulty reaching/extending arms up
gen s5armsa =.
spouse r5armsa, result(s5armsa) wave(5)
label variable s5armsa "s5armsa:w5 S Some Diff-Rch/xtnd arms up"
label values s5armsa adldiff

***Difficulty pushing/pulling a large object***
*wave 5 respondent difficulty pushing/pulling a large object
gen r5pusha =.
missing_common ph048d8, result(r5pusha)
replace r5pusha = 0 if ph048d8==0
replace r5pusha = 1 if ph048d8==1
label variable r5pusha "r5pusha:w5 R Some Diff-Push/pull lg obj"
label values r5pusha adldiff

*wave 5 spouse difficulty pushing/pulling a large object
gen s5pusha =.
spouse r5pusha, result(s5pusha) wave(5)
label variable s5pusha "s5pusha:w5 S Some Diff-Push/pull lg obj"
label values s5pusha adldiff

***ADL Summary 0-5***
*Wave 5 respondent ADL number of missings
egen r5adlam = rowmiss(r5batha r5dressa r5eata r5beda r5walkra) if inw5 == 1
label variable r5adlam "r5adlam:w5 Missings in ADL summary"

*wave 5 spouse ADL number of missings
gen s5adlam =.
spouse r5adlam, result(s5adlam) wave(5)
label variable s5adlam "s5adlam:w5 Missings in ADL summary"

local adl
foreach v of varlist r5batha r5dressa r5eata r5beda r5walkra {
	recode `v' (miss = 0), generate(`v'_m)
	local adl `adl' `v'_m
}

*Wave 5 respondent ADL Summary 0-5
gen r5adla =.
missing_h r5batha r5dressa r5eata r5beda r5walkra, result(r5adla)
replace r5adla = r5batha_m + r5dressa_m + r5eata_m + r5beda_m + r5walkra_m if inrange(r5adlam,0,4)
label variable r5adla "r5adla:w5 R Some Diff-ADLs /0-5"

*wave 5 spouse ADL Summary 0-5
gen s5adla =.
spouse r5adla, result(s5adla) wave(5)
label variable s5adla "s5adla:w5 S Some Diff-ADLs /0-5"

*drop intermediate variables
drop `adl'

***ADL Wallace Summary 0-3***
*Wave 5 respondent ADL Wallace number of missings
egen r5adlwam = rowmiss(r5batha r5dressa r5eata) if inw5 == 1
label variable r5adlwam "r5adlwam:w5 r Missings in ADL Wallace summary"

*wave 5 spouse ADL Wallace number of missings
gen s5adlwam =.
spouse r5adlwam, result(s5adlwam) wave(5)
label variable s5adlwam "s5adlwam:w5 s Missings in ADL Wallace summary"

local adlw
foreach v of varlist r5batha r5dressa r5eata {
	recode `v' (miss = 0), generate(`v'_m)
	local adlw `adlw' `v'_m
}

*Wave 5 respondent ADL Wallace Summary 0-3
gen r5adlwa =.
missing_h r5batha r5dressa r5eata, result(r5adlwa)
replace r5adlwa = r5batha_m + r5dressa_m + r5eata_m if inrange(r5adlwam,0,2)
label variable r5adlwa "r5adlwa:w5 R Some Diff-ADLs:Wallace /0-3"

*wave 5 spouse ADL Wallace Summary 0-3
gen s5adlwa =.
spouse r5adlwa, result(s5adlwa) wave(5)
label variable s5adlwa "s5adlwa:w5 S Some Diff-ADLs:Wallace /0-3"

*drop intermediate variables
drop `adlw'

***IADL Summary 0-3***
*Wave 5 respondent IADL number of missings
egen r5iadlam = rowmiss(r5phonea r5moneya r5medsa) if inw5 == 1
label variable r5iadlam "r5iadlam:w5 r Missings in IADL 0-3 summary"

*wave 5 spouse IADL number of missings
gen s5iadlam =.
spouse r5iadlam, result(s5iadlam) wave(5)
label variable s5iadlam "s5iadlam:w5 s Missings in IADL 0-3 summary"

local iadl
foreach v of varlist r5phonea r5moneya r5medsa {
	recode `v' (miss = 0), generate(`v'_m)
	local iadl `iadl' `v'_m
} 

*Wave 5 respondent IADLs Summary 0-3
gen r5iadla =.
missing_h r5phonea r5moneya r5medsa, result(r5iadla)
replace r5iadla = r5phonea_m + r5moneya_m + r5medsa_m if inrange(r5iadlam,0,2)
label variable r5iadla "r5iadla:w5 R Some Diff-IADLs: /0-3"

*wave 5 spouse IADLs Summary 0-3
gen s5iadla =.
spouse r5iadla, result(s5iadla) wave(5)
label variable s5iadla "s5iadla:w5 S Some Diff-IADLs: /0-3"

*drop intermediate variables
drop `iadl'

***IADL Summary 0-5***
*Wave 5 respondent IADL number of missings
egen r5iadlzam = rowmiss(r5phonea r5moneya r5medsa r5shopa r5mealsa) if inw5 == 1
label variable r5iadlzam "r5iadlzam:w5 r Missings in IADL 0-5 summary"

*wave 5 spouse IADL number of missings
gen s5iadlzam =.
spouse r5iadlzam, result(s5iadlzam) wave(5)
label variable s5iadlzam "s5iadlzam:w5 s Missings in IADL 0-5 summary"

local iadlz
foreach v of varlist r5phonea r5moneya r5medsa r5shopa r5mealsa {
	recode `v' (miss = 0), generate(`v'_m)
	local iadlz `iadlz' `v'_m
} 

*Wave 5 respondent IADLs Summary 0-5
gen r5iadlza =.
missing_h r5phonea r5moneya r5medsa r5shopa r5mealsa, result(r5iadlza)
replace r5iadlza = r5phonea_m + r5moneya_m + r5medsa_m + r5shopa_m + r5mealsa_m if inrange(r5iadlzam,0,4)
label variable r5iadlza "r5iadlza:w5 R Some Diff-IADLs: /0-5"

*wave 5 spouse IADLs Summary 0-5
gen s5iadlza =.
spouse r5iadlza, result(s5iadlza) wave(5)
label variable s5iadlza "s5iadlza:w5 S Some Diff-IADLs: /0-5"

*drop intermediate variables
drop `iadlz'

***Mobility Summary***
*Wave 5 respondent Mobility Summary number of missings
egen r5mobilbm = rowmiss(r5walk100a r5walkra r5climsa r5clim1a) if inw5 == 1
label variable r5mobilbm "r5mobilbm:w5 r Missings in mobility summary"

*wave 5 spouse Mobility Summary number of missings
gen s5mobilbm =.
spouse r5mobilbm, result(s5mobilbm) wave(5)
label variable s5mobilbm "s5mobilbm:w5 s Missings in mobility summary"

local mobilb
foreach v of varlist r5walk100a r5walkra r5climsa r5clim1a {
	recode `v' (miss = 0), generate(`v'_m)
	local mobilb `mobilb' `v'_m
} 

*Wave 5 respondent Mobility Summary
gen r5mobilb =.
missing_h r5walk100a r5walkra r5climsa r5clim1a, result(r5mobilb)
replace r5mobilb = r5walk100a_m + r5walkra_m + r5climsa_m + r5clim1a_m if inrange(r5mobilbm,0,3)
label variable r5mobilb "r5mobilb:w5 R Some Diff-Mobility /0-4"

*wave 5 spouse Mobility Summary
gen s5mobilb =.
spouse r5mobilb, result(s5mobilb) wave(5)
label variable s5mobilb "s5mobilb:w5 S Some Diff-Mobility /0-4"

*drop intermediate variables
drop `mobilb'

***Large Muscle Summary***
*Wave 5 respondent Large Muscle Summary number of missings
egen r5lgmusam = rowmiss(r5sita r5chaira r5stoopa r5pusha) if inw5 == 1
label variable r5lgmusam "r5lgmusam:w5 r Missings in large muscle summary"

*wave 5 spouse Large Muscle Summary number of missings
gen s5lgmusam =.
spouse r5lgmusam, result(s5lgmusam) wave(5)
label variable s5lgmusam "s5lgmusam:w5 s Missings in large muscle summary"

local lgmusa
foreach v of varlist r5sita r5chaira r5stoopa r5pusha {
	recode `v' (miss = 0), generate(`v'_m)
	local lgmusa `lgmusa' `v'_m
} 

*Wave 5 respondent Large Muscle Summary
gen r5lgmusa =.
missing_h r5sita r5chaira r5stoopa r5pusha, result(r5lgmusa)
replace r5lgmusa = r5sita_m + r5chaira_m + r5stoopa_m + r5pusha_m if inrange(r5lgmusam,0,3)
label variable r5lgmusa "r5lgmusa:w5 R Some Diff-Large muscle /0-4"

*wave 5 spouse Large Muscle Summary
gen s5lgmusa =.
spouse r5lgmusa, result(s5lgmusa) wave(5)
label variable s5lgmusa "s5lgmusa:w5 S Some Diff-Large muscle /0-4"

*drop intermediate variables
drop `lgmusa'

***Gross Motor Summary***
*Wave 5 respondent Gross Motor Summary number of missings
egen r5grossam = rowmiss(r5walk100a r5walkra r5clim1a r5beda r5batha) if inw5 == 1
label variable r5grossam "r5grossam:w5 r Missings in wk,rn,clmb,bd,bth summary"

*wave 5 spouse Gross Motor Summary number of missings
gen s5grossam =.
spouse r5grossam, result(s5grossam) wave(5)
label variable s5grossam "s5grossam:w5 s Missings in wk,rn,clmb,bd,bth summary"

local grossa
foreach v of varlist r5walk100a r5walkra r5clim1a r5beda r5batha {
	recode `v' (miss = 0), generate(`v'_m)
	local grossa `grossa' `v'_m
}

*Wave 5 respondent Gross Motor Summary
gen r5grossa =.
missing_h r5walk100a r5walkra r5clim1a r5beda r5batha, result(r5grossa)
replace r5grossa = r5walk100a_m + r5walkra_m + r5clim1a_m + r5beda_m + r5batha_m if inrange(r5grossam,0,4)
label variable r5grossa "r5grossa:w5 R Some Diff-Wk,rn,clmb,bd,bth /0-5"

*wave 5 spouse Gross Motor Summary
gen s5grossa =.
spouse r5grossa, result(s5grossa) wave(5)
label variable s5grossa "s5grossa:w5 S Some Diff-Wk,rn,clmb,bd,bth /0-5"

*drop intermediate variables
drop `grossa'

***Fine Motor Summary***
*Wave 5 respondent Fine Motor Summary number of missings
egen r5fineam = rowmiss(r5dimea r5eata r5dressa) if inw5 == 1
label variable r5fineam "r5fineam:w5 r Missings in dime,eat,dress summary"

*wave 5 spouse Gross Fine Summary number of missings
gen s5fineam =.
spouse r5fineam, result(s5fineam) wave(5)
label variable s5fineam "s5fineam:w5 s Missings in dime,eat,dress summary"

local finea
foreach v of varlist r5dimea r5eata r5dressa {
	recode `v' (miss = 0), generate(`v'_m)
	local finea `finea' `v'_m
}

*Wave 5 respondent Fine Motor Summary
gen r5finea =.
missing_h r5dimea r5eata r5dressa, result(r5finea)
replace r5finea = r5dimea_m + r5eata_m + r5dressa_m if inrange(r5fineam,0,2)
label variable r5finea "r5finea:w5 R Some Diff-dime,eat,dress /0-3"

*wave 5 spouse Fine Motor Summary
gen s5finea =.
spouse r5finea, result(s5finea) wave(5)
label variable s5finea "s5finea:w5 S Some Diff-dime,eat,dress /0-3"

*drop intermediate variables
drop `finea'

***EURO-D: Depression***
*wave 5 respondent depression
gen r5depress =.
missing_common euro1, result(r5depress)
replace r5depress = euro1 if inlist(euro1,0,1)
label variable r5depress "r5depress:w5 EURO-D: r Depression (0,1)"
label values r5depress heurod

*wave 5 spouse depression
gen s5depress =.
spouse r5depress, result(s5depress) wave(5)
label variable s5depress "s5depress:w5 EURO-D: s Depression (0,1)"
label values s5depress heurod

***EURO-D: Pessimism***
*wave 5 respondent pessimism
gen r5pessim =.
missing_common euro2, result(r5pessim)
replace r5pessim = euro2 if inlist(euro2,0,1)
label variable r5pessim "r5pessim:w5 EURO-D: r Pessimism (0,1)"
label values r5pessim heurod

*wave 5 spouse pessimism
gen s5pessim =.
spouse r5pessim, result(s5pessim) wave(5)
label variable s5pessim "s5pessim:w5 EURO-D: s Pessimism (0,1)"
label values s5pessim heurod

***EURO-D: Suicidality***
*wave 5 respondent suicidality
gen r5suicid =.
missing_common euro3, result(r5suicid)
replace r5suicid = euro3 if inlist(euro3,0,1)
label variable r5suicid "r5suicid:w5 EURO-D: r Suicidality (0,1)"
label values r5suicid heurod

*wave 5 spouse suicidality
gen s5suicid =.
spouse r5suicid, result(s5suicid) wave(5)
label variable s5suicid "s5suicid:w5 EURO-D: s Suicidality (0,1)"
label values s5suicid heurod

***EURO-D: Guilt***
*wave 5 respondent guilt
gen r5guilt =.
missing_common euro4, result(r5guilt)
replace r5guilt = euro4 if inlist(euro4,0,1)
label variable r5guilt "r5guilt:w5 EURO-D: r Guilt (0,1)"
label values r5guilt heurod

*wave 5 spouse guilt
gen s5guilt =.
spouse r5guilt, result(s5guilt) wave(5)
label variable s5guilt "s5guilt:w5 EURO-D: s Guilt (0,1)"
label values s5guilt heurod

***EURO-D: Sleep***
*wave 5 respondent sleep
gen r5sleep =.
missing_common euro5, result(r5sleep)
replace r5sleep = euro5 if inlist(euro5,0,1)
label variable r5sleep "r5sleep:w5 EURO-D: r Sleep (0,1)"
label values r5sleep heurod

*wave 5 spouse sleep
gen s5sleep =.
spouse r5sleep, result(s5sleep) wave(5)
label variable s5sleep "s5sleep:w5 EURO-D: s Sleep (0,1)"
label values s5sleep heurod

***EURO-D: Interest***
*wave 5 respondent interest
gen r5intrst =.
missing_common euro6, result(r5intrst)
replace r5intrst = euro6 if inlist(euro6,0,1)
label variable r5intrst "r5intrst:w5 EURO-D: r Interest (0,1)"
label values r5intrst heurod

*wave 5 spouse interest
gen s5intrst =.
spouse r5intrst, result(s5intrst) wave(5)
label variable s5intrst "s5intrst:w5 EURO-D: s Interest (0,1)"
label values s5intrst heurod

***EURO-D: Irritability***
*wave 5 respondent irritability
gen r5irritb =.
missing_common euro7, result(r5irritb)
replace r5irritb = euro7 if inlist(euro7,0,1)
label variable r5irritb "r5irritb:w5 EURO-D: r Irritability (0,1)"
label values r5irritb heurod

*wave 5 spouse irritability
gen s5irritb =.
spouse r5irritb, result(s5irritb) wave(5)
label variable s5irritb "s5irritb:w5 EURO-D: s Irritability (0,1)"
label values s5irritb heurod

***EURO-D: Appetite***
*wave 5 respondent appetite
gen r5appett =.
missing_common euro8, result(r5appett)
replace r5appett = euro8 if inlist(euro8,0,1)
label variable r5appett "r5appett:w5 EURO-D: r Appetite (0,1)"
label values r5appett heurod

*wave 5 spouse appetite
gen s5appett =.
spouse r5appett, result(s5appett) wave(5)
label variable s5appett "s5appett:w5 EURO-D: s Appetite (0,1)"
label values s5appett heurod

***EURO-D: Fatigue***
*wave 5 respondent fatigue
gen r5fatig =.
missing_common euro9, result(r5fatig)
replace r5fatig = euro9 if inlist(euro9,0,1)
label variable r5fatig "r5fatig:w5 EURO-D: r Fatigue (0,1)"
label values r5fatig heurod

*wave 5 spouse fatigue
gen s5fatig =.
spouse r5fatig, result(s5fatig) wave(5)
label variable s5fatig "s5fatig:w5 EURO-D: s Fatigue (0,1)"
label values s5fatig heurod

***EURO-D: Concentration***
*wave 5 respondent concentration
gen r5concnt =.
missing_common euro10, result(r5concnt)
replace r5concnt = euro10 if inlist(euro10,0,1)
label variable r5concnt "r5concnt:w5 EURO-D: r Concentration (0,1)"
label values r5concnt heurod

*wave 5 spouse concentration
gen s5concnt =.
spouse r5concnt, result(s5concnt) wave(5)
label variable s5concnt "s5concnt:w5 EURO-D: s Concentration (0,1)"
label values s5concnt heurod

***EURO-D: Enjoyment***
*wave 5 respondent enjoyment
gen r5enjoym =.
missing_common euro11, result(r5enjoym)
replace r5enjoym = euro11 if inlist(euro11,0,1)
label variable r5enjoym "r5enjoym:w5 EURO-D: r Enjoyment (0,1)"
label values r5enjoym heurod

*wave 5 spouse enjoyment
gen s5enjoym =.
spouse r5enjoym, result(s5enjoym) wave(5)
label variable s5enjoym "s5enjoym:w5 EURO-D: s Enjoyment (0,1)"
label values s5enjoym heurod

***EURO-D: Tearfulness***
*wave 5 respondent tearfulness
gen r5tearfl =.
missing_common euro12, result(r5tearfl)
replace r5tearfl = euro12 if inlist(euro12,0,1)
label variable r5tearfl "r5tearfl:w5 EURO-D: r Tearfulness (0,1)"
label values r5tearfl heurod

*wave 5 spouse tearfulness
gen s5tearfl =.
spouse r5tearfl, result(s5tearfl) wave(5)
label variable s5tearfl "s5tearfl:w5 EURO-D: s Tearfulness (0,1)"
label values s5tearfl heurod

***EURO-D: Score***
*wave 5 respondent EURO-D score
gen r5eurod =.
replace r5eurod = eurod
label variable r5eurod "r5eurod:w5 EURO-D: r Score"

*wave 5 respondent flag for EURO-D score
w5_imputation_flag eurod_f, gen(r5feurod)
label variable r5feurod "r5feurod:w5 EURO-D Flag: r Score"
label values r5feurod healthimpflag

*wave 5 spouse EURO-D score
gen s5eurod =.
spouse r5eurod, result(s5eurod) wave(5)
label variable s5eurod "s5eurod:w5 EURO-D: s Score"

*wave 5 spouse flag for EURO-D score
gen s5feurod =.
spouse r5feurod, result(s5feurod) wave(5)
label variable s5feurod "s5feurod:w5 EURO-D Flag: s Score"
label values s5feurod healthimpflag

***Ever had high blood pressure***
*wave 5 respondent ever had high blood pressure
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'hibpe == 1
}

gen r`wv'hibpe =.
missing_common ph006d2, result(r`wv'hibpe)
replace r`wv'hibpe = 0 if ph006d2 == 0
replace r`wv'hibpe = 1 if (ph006d2 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'hibpe "r`wv'hibpe:w`wv' r ever had high blood pressure"
label values r`wv'hibpe health_problems

*wave 5 spouse ever had high blood pressure
gen s5hibpe =.
spouse r5hibpe, result(s5hibpe) wave(5)
label variable s5hibpe "s5hibpe:w5 s ever had high blood pressure"
label values s5hibpe health_problems

***Ever had diabetes***
*wave 5 respondent ever had diabetes
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'diabe == 1
}

gen r`wv'diabe =.
missing_common ph006d5, result(r`wv'diabe)
replace r`wv'diabe = 0 if ph006d5 == 0 
replace r`wv'diabe = 1 if (ph006d5 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'diabe "r`wv'diabe:w`wv' r ever had diabetes"
label values r`wv'diabe health_problems

*wave 5 spouse ever had diabetes
gen s5diabe =.
spouse r5diabe, result(s5diabe) wave(5)
label variable s5diabe "s5diabe:w5 s ever had diabetes"
label values s5diabe health_problems

***Ever had cancer***
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'cancre == 1
}

gen preload = .
foreach w in `wvs_before' {
    replace preload = r`w'cancre if inw`w' == 1 & preload == .
}

*wave 5 respondent ever had cancer
gen r`wv'cancre =.
missing_common ph006d10, result(r`wv'cancre)
replace r`wv'cancre = 0 if ph006d10 == 0
replace r`wv'cancre = 1 if (ph006d10 == 1 | ph072_3 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'cancre "r`wv'cancre:w`wv' r ever had cancer"
label values r`wv'cancre health_problems

*wave 5 respondent cancer dispute flag
gen r`wv'cancrf = .
replace r`wv'cancrf = 0 if (inlist(ph006d10,0,1) | ph072_3 == 1 `or_reported_earlier') & inw`wv' == 1
replace r`wv'cancrf = 2 if ph073_3 == 5 & preload == 1
replace r`wv'cancrf = 3 if ph073_3 == 5 & preload == 0
label variable r`wv'cancrf "r`wv'cancrf:w`wv' r cancer dispute flag"
label values r`wv'cancrf ehealth_problems_flag

*update previous dispute flag
foreach w in `wvs_before' {
    replace r`w'cancrf = 5 if ph073_3 == 5 & r`w'cancre == 1 & r`w'cancrf == 0
    replace r`w'cancrf = 6 if ph073_3 == 5 & r`w'cancre == 0 & r`w'cancrf == 0
}

*update previous ever have variables
foreach w in `wvs_before' {
    replace r`w'cancre = 0 if ph073_3 == 5 & r`w'cancre == 1
}

drop preload

***Ever had lung disease***
*wave 5 respondent ever had lung disease
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'lunge == 1
}

gen r`wv'lunge =.
missing_common ph006d6, result(r`wv'lunge)
replace r`wv'lunge = 0 if ph006d6 == 0 
replace r`wv'lunge = 1 if (ph006d6 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'lunge "r`wv'lunge:w`wv' r ever had lung disease"
label values r`wv'lunge health_problems

*wave 5 spouse ever had lung disease
gen s5lunge =.
spouse r5lunge, result(s5lunge) wave(5)
label variable s5lunge "s5lunge:w5 s ever had lung disease"
label values s5lunge health_problems

***Ever had heart problems***
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'hearte == 1
}

gen preload = .
foreach w in `wvs_before' {
    replace preload = r`w'hearte if inw`w' == 1 & preload == .
}

*wave 5 respondent ever had heart problems
gen r`wv'hearte =.
missing_common ph006d1, result(r`wv'hearte)
replace r`wv'hearte = 0 if ph006d1 == 0
replace r`wv'hearte = 1 if (ph006d1 == 1  | ph072_1 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'hearte "r`wv'hearte:w`wv' r ever had heart problems"
label values r`wv'hearte health_problems

*wave 5 respondent heart problems dispute flag
gen r`wv'heartf = .
replace r`wv'heartf = 0 if (inlist(ph006d1,0,1) | ph072_1 == 1 `or_reported_earlier') & inw`wv' == 1
replace r`wv'heartf = 2 if ph073_1 == 5 & preload == 1
replace r`wv'heartf = 3 if ph073_1 == 5 & preload == 0
label variable r`wv'heartf "r`wv'heartf:w`wv' r heart problems dispute flag"
label values r`wv'heartf ehealth_problems_flag

*update previous dispute flag
foreach w in `wvs_before' {
    replace r`w'heartf = 5 if ph073_1 == 5 & r`w'hearte == 1 & r`w'heartf == 0
    replace r`w'heartf = 6 if ph073_1 == 5 & r`w'hearte == 0 & r`w'heartf == 0
}

*update previous ever have variables
foreach w in `wvs_before' {
    replace r`w'hearte = 0 if ph073_1 == 5 & r`w'hearte == 1
}

drop preload

***Ever had stroke***
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'stroke == 1
}

gen preload = .
foreach w in `wvs_before' {
    replace preload = r`w'stroke if inw`w' == 1 & preload == .
}

*wave 5 respondent ever had stroke
gen r`wv'stroke =.
missing_common ph006d4 , result(r`wv'stroke)
replace r`wv'stroke = 0 if ph006d4 == 0
replace r`wv'stroke = 1 if (ph006d4 == 1 | ph072_2 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'stroke "r`wv'stroke:w`wv' r ever had stroke"
label values r`wv'stroke health_problems

*wave 5 respondent stroke dispute flag
gen r`wv'strokf = .
replace r`wv'strokf = 0 if (inlist(ph006d4,0,1) | ph072_2 == 1 `or_reported_earlier') & inw`wv' == 1
replace r`wv'strokf = 2 if ph073_2 == 5 & preload == 1
replace r`wv'strokf = 3 if ph073_2 == 5 & preload == 0
label variable r`wv'strokf "r`wv'strokf:w`wv' r stroke dispute flag"
label values r`wv'strokf ehealth_problems_flag

*update previous dispute flag
foreach w in `wvs_before' {
    replace r`w'strokf = 5 if ph073_2 == 5 & r`w'stroke == 1 & r`w'strokf == 0
    replace r`w'strokf = 6 if ph073_2 == 5 & r`w'stroke == 0 & r`w'strokf == 0
}

*update previous ever have variables
foreach w in `wvs_before' {
    replace r`w'stroke = 0 if ph073_2 == 5 & r`w'stroke == 1
}

drop preload

***Ever had arthritis***
*wave 5 respondent ever had arthritis
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'arthre == 1
}

gen r`wv'arthre =.
missing_common ph006d19 ph006d20, result(r`wv'arthre)
replace r5arthre = 0 if ph006d19 == 0 & ph006d20 == 0
replace r5arthre = 1 if (ph006d19 == 1 | ph006d20 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'arthre "r`wv'arthre:w`wv' r ever had arthritis"
label values r`wv'arthre health_problems

*wave 5 spouse ever had arthritis
gen s5arthre =.
spouse r5arthre, result(s5arthre) wave(5)
label variable s5arthre "s5arthre:w5 s ever had arthritis"
label values s5arthre health_problems

***Ever had high cholesterol***
*wave 5 respondent ever had high cholesterol
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'hchole == 1
}

gen r`wv'hchole = .
missing_common ph006d3, result(r`wv'hchole)
replace r`wv'hchole = ph006d3 if inrange(ph006d3,0,1)
label variable r`wv'hchole "r`wv'hchole:w`wv' r ever had high cholesterol"
label values r`wv'hchole health_problems

*wave 5 spouse ever had high cholesterol
gen s`wv'hchole = .
spouse r`wv'hchole, result(s`wv'hchole) wave(`wv')
label variable s`wv'hchole "s`wv'hchole:w`wv' s ever had high cholesterol"
label values s`wv'hchole health_problems

***Ever had Parkinsons***
*wave 5 respondent ever had parkinsons
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'parkine == 1
}

gen r`wv'parkine = .
missing_common ph006d12, result(r`wv'parkine)
replace r`wv'parkine = ph006d12 if inrange(ph006d12,0,1)
label variable r`wv'parkine "r`wv'parkine:w`wv' r ever had parkinson disease"
label values r`wv'parkine health_problems

*wave 5 spouse ever had parkinsons
gen s`wv'parkine = .
spouse r`wv'parkine, result(s`wv'parkine) wave(`wv')
label variable s`wv'parkine "s`wv'parkine:w`wv' s ever had parkinson disease"
label values s`wv'parkine health_problems

***Ever had cataracts***
*wave 5 respondent ever had cataracts
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'catrcte == 1
}

gen r`wv'catrcte = .
missing_common ph006d13, result(r`wv'catrcte)
replace r`wv'catrcte = ph006d13 if inrange(ph006d13,0,1)
label variable r`wv'catrcte "r`wv'catrcte:w`wv' r ever had cataracts"
label values r`wv'catrcte health_problems

*wave 5 spouse ever had cataracts
gen s`wv'catrcte = .
spouse r`wv'catrcte, result(s`wv'catrcte) wave(`wv')
label variable s`wv'catrcte "s`wv'catrcte:w`wv' s ever had cataracts"
label values s`wv'catrcte health_problems

***Ever had hip or femoral fracture***
*wave 5 respondent ever had hip or femoral fracture
local or_reported_earlier
foreach w in `wvs_before' {
	local or_reported_earlier `or_reported_earlier' | r`w'hipfeme == 1
}

gen preload = .
foreach w in `wvs_before' {
	replace preload = r`w'hipfeme if inw`w' == 1 & preload == .
}

*wave 5 respondent ever had hip or femoral fracture
gen r`wv'hipfeme = .
missing_common ph006d14, result(r`wv'hipfeme)
replace r`wv'hipfeme = 0 if ph006d14 == 0
replace r`wv'hipfeme = 1 if (ph006d14 == 1 | ph072_4 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'hipfeme "r`wv'hipfeme:w`wv' r ever had hip/femoral fracture"
label values r`wv'hipfeme health_problems

*wave 5 respondent hip/femoral fracture dispute flag
gen r`wv'hipfemf = .
replace r`wv'hipfemf = 0 if (inlist(ph006d14,0,1) | ph072_4 == 1 `or_reported_earlier') & inw`wv' == 1
replace r`wv'hipfemf = 2 if ph073_4 == 5 & preload == 1
replace r`wv'hipfemf = 3 if ph073_4 == 5 & preload == 0
label variable r`wv'hipfemf "r`wv'hipfemf:w`wv' r hip/femoral fracture dispute flag"
label values r`wv'hipfemf ehealth_problems_flag

*update previous dispute flag
foreach w in `wvs_before' {
	replace r`w'hipfemf = 5 if ph073_4 == 5 & r`w'hipfeme == 1 & r`w'hipfemf == 0
	replace r`w'hipfemf = 6 if ph073_4 == 5 & r`w'hipfeme == 0 & r`w'hipfemf == 0
}

*update previous ever have variables
foreach w in `wvs_before' {
	replace r`w'hipfeme = 0 if ph073_4 == 5 & r`w'hipfeme == 1
}

drop preload

***Ever had psychiatric disorder***
*wave 5 respondent ever had psych disorder
local or_reported_earlier
foreach w in 2 4 {
    local or_reported_earlier `or_reported_earlier' | r`w'psyche == 1
}

gen r5psyche = .
missing_common ph006d18, result(r5psyche)
replace r5psyche = 0 if ph006d18 == 0 
replace r5psyche = 1 if (ph006d18 == 1 `or_reported_earlier') & inw`wv' == 1 
label variable r5psyche "r5psyche:w5 r ever had psych disorder"
label values r5psyche health_problems

*wave 5 spouse ever had psych disorder
gen s5psyche = .
spouse r5psyche, result(s5psyche) wave(5)
label variable s5psyche "s5psyche:w5 s ever had psych disorder"
label values s5psyche health_problems

***Ever had ulcer***
*wave 5 respondent ever had ulcer
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'ulcer == 1
}

gen r`wv'ulcer =.
missing_common ph006d11, result(r`wv'ulcer)
replace r`wv'ulcer = 0 if ph006d11 == 0
replace r`wv'ulcer = 1 if (ph006d11 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'ulcer "r`wv'ulcer:w`wv' r ever had ulcer"
label values r`wv'ulcer health_problems

*wave 5 spouse ever had ulcer
gen s5ulcer =.
spouse r5ulcer, result(s5ulcer) wave(5)
label variable s5ulcer "s5ulcer:w5 s ever had ulcer"
label values s5ulcer health_problems

***Ever had Alzheimerís disease, dementia***
*wave 5 respondent ever had Alzheimerís disease, dementia
local or_reported_earlier
foreach w in 4 2 {
    local or_reported_earlier `or_reported_earlier' | r`w'alzdem == 1
}

gen r`wv'alzdem =.
missing_common ph006d16 , result(r`wv'alzdem)
replace r`wv'alzdem = 0 if ph006d16 == 0
replace r`wv'alzdem = 1 if (ph006d16 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'alzdem "r`wv'alzdem:w`wv' r ever had alzheimer's/dementia"
label values r`wv'alzdem health_problems

*wave 5 spouse ever had Alzheimerís disease, dementia
gen s5alzdem =.
spouse r5alzdem, result(s5alzdem) wave(5)
label variable s5alzdem "s5alzdem:w5 s ever had alzheimer's/dementia"
label values s5alzdem health_problems

***Height in meters***
*wave 5 respondent height in meters
gen r5height =.
missing_common ph013_, result(r5height)
replace r5height = .i if inrange(ph013_,0,89) 
replace r5height = ph013_ if inrange(ph013_,1.00,1.90) & inw5==1
replace r5height = ph013_/100 if inrange(ph013_,90,220) & inw5==1
label variable r5height "r5height:w5 r Height in meters"

*wave 5 spouse height in meters
gen s5height =.
spouse r5height, result(s5height) wave(5)
label variable s5height "s5height:w5 s Height in meters"

***Weight in kilograms***
*wave 5 respondent weight in kilograms
gen r5weight =.
missing_common ph012_, result(r5weight)
replace r5weight = .i if inrange(ph012_,0,12) 
replace r5weight = ph012_ if inrange(ph012_,13,250) 
label variable r5weight "r5weight:w5 r Weight in kilograms"

*wave 5 spouse weight in kilograms
gen s5weight =.
spouse r5weight, result(s5weight) wave(5)
label variable s5weight "s5weight:w5 s Weight in kilograms"

***BMI***
*wave 5 respondent BMI
gen r5bmi =.
missing_h r5weight r5height, result(r5bmi)
replace r5bmi = r5weight/(r5height^2) if !mi(r5weight) & !mi(r5height)
replace r5bmi = .i if (r5height==.i | r5weight==.i | !inrange(r5bmi,12,100)) & inw`wv' == 1
label variable r5bmi "r5bmi:w5 r Body Mass Index=kg/m2"

*wave 5 spouse BMI
gen s5bmi =.
spouse r5bmi, result(s5bmi) wave(5)
label variable s5bmi "s5bmi:w5 s Body Mass Index=kg/m2"

***BMI Categorization***
*wave 5 respondent BMI categorization
gen r5bmicat = .
missing_h r5weight r5height, result(r5bmicat)
replace r5bmicat = 1 if inrange(r5bmi,1,18.49999)
replace r5bmicat = 2 if inrange(r5bmi,18.50,24.99999)
replace r5bmicat = 3 if inrange(r5bmi,25,29.99999)
replace r5bmicat = 4 if inrange(r5bmi,30,34.99999)
replace r5bmicat = 5 if inrange(r5bmi,35,39.99999)
replace r5bmicat = 6 if inrange(r5bmi,40,100)
replace r5bmicat = .i if r5weight==.i | r5height==.i | r5bmi==.i
label variable r5bmicat "r5bmicat:w5 r BMI categorization"
label values r5bmicat bmicate

*wave 5 spouse BMI categorization
gen s5bmicat = .
spouse r5bmicat, result(s5bmicat) wave(5)
label variable s5bmicat "s5bmicat:w5 s BMI categorization"
label values s5bmicat bmicate

***Vigorous physical activity***
*wave 5 respondent vigorous physical activity
gen r5vgactx =.
missing_common br015_, result(r5vgactx)
replace r5vgactx = 2 if br015_ == 1
replace r5vgactx = 3 if br015_ == 2
replace r5vgactx = 4 if br015_ == 3
replace r5vgactx = 5 if br015_ == 4
label variable r5vgactx "r5vgactx:w5 R Freq vigorous phys activ"
label values r5vgactx activity

*wave 5 spouse vigorous physical activity
gen s5vgactx =.
spouse r5vgactx, result(s5vgactx) wave(5)
label variable s5vgactx "s5vgactx:w5 S Freq vigorous phys activ"
label values s5vgactx activity

***Moderate physical activity***
*wave 5 respondent moderate physical activity
gen r5mdactx =.
missing_common br016_, result(r5mdactx)
replace r5mdactx = 2 if br016_ == 1
replace r5mdactx = 3 if br016_ == 2
replace r5mdactx = 4 if br016_ == 3
replace r5mdactx = 5 if br016_ == 4
label variable r5mdactx "r5mdactx:w5 R Freq moderate phys activ"
label values r5mdactx activity

*wave 5 spouse vigorous moderate activity
gen s5mdactx =.
spouse r5mdactx, result(s5mdactx) wave(5)
label variable s5mdactx "s5mdactx:w5 S Freq moderate phys activ"
label values s5mdactx activity

***Ever drinks alcohol***
**change to last 7 days
*wave 5 respondent ever drinks alcohol
gen r5drink =.
missing_common br010_, result(r5drink)
replace r5drink = 0 if inlist(br010_,5,6,7)
replace r5drink = 1 if inlist(br010,1,2,3,4)
label variable r5drink "r5drink:w5 R ever drinks any alcohol"
label values r5drink ever_drink

*wave 5 spouse ever drinks alcohol
gen s5drink =.
spouse r5drink, result(s5drink) wave(5)
label variable s5drink "s5drink:w5 S ever drinks any alcohol"
label values s5drink ever_drink

***Days per week drinks***
*wave 5 respondent days per week drinks
gen r5drinkx =.
missing_common br010_, result(r5drinkx)
replace r5drinkx = 0 if inlist(br010_,6,7)
replace r5drinkx = 1 if br010_ == 5
replace r5drinkx = 2 if inlist(br010_,4,3) 
replace r5drinkx = 3 if br010_ == 2 
replace r5drinkx = 4 if br010_ == 1 
label variable r5drinkx "r5drinkx:w5 R Frequency of drinking"
label values r5drinkx drinkx

*wave 5 spouse days per week drinks
gen s5drinkx =.
spouse r5drinkx, result(s5drinkx) wave(5)
label variable s5drinkx "s5drinkx:w5 S Frequency of drinking"
label values s5drinkx drinkx

***No. Drinks per day when drinks***
*wave 5 respondent no. drinks per day when drinks
gen r5drinkn = .
missing_common br019_, result(r5drinkn)
replace r5drinkn = br019_ if inrange(br019_,0,70)
replace r5drinkn = 0 if br010_ == 7
label variable r5drinkn "r5drinkn:w5 r no. drinks per day"

*wave 5 spouse no. drinks per day when drinks
gen s5drinkn = .
spouse r5drinkn, result(s5drinkn) wave(5)
label variable s5drinkn "s5drinkn:w5 s no. drinks per day"

***Ever smoked cigarettes***
*wave 5 respondent ever smoked cigarettes
gen r5smokev =.
missing_common br001_, result(r5smokev)
replace r5smokev = r1smokev if !mi(r1smokev) & inw5 == 1
replace r5smokev = r2smokev if !mi(r2smokev) & inw5 == 1
replace r5smokev = r4smokev if !mi(r4smokev) & inw5 == 1
replace r5smokev = 0 if br001_ == 5
replace r5smokev = 1 if br001_ == 1 | br002_ == 1
label variable r5smokev "r5smokev:w5 R smoke ever"
label values r5smokev smokes

*wave 5 spouse ever smoked cigarettes
gen s5smokev =.
spouse r5smokev, result(s5smokev) wave(5)
label variable s5smokev "s5smokev:w5 S smoke ever"
label values s5smokev smokes

***Smokes cigarettes now***
*wave 5 respondent smokes cigarettes now
***check br022_ 
gen r5smoken =.
missing_common br001_ br002_, result(r5smoken)
replace r5smoken = 0 if br001_ == 5 | (br002_ == 5 & br022_ != 3)
replace r5smoken = 1 if br002_ == 1 | br022_ == 3
label variable r5smoken "r5smoken:w5 R smokes now"
label values r5smoken smokes

*wave 5 spouse smokes cigarettes now
gen s5smoken =.
spouse r5smoken, result(s5smoken) wave(5)
label variable s5smoken "s5smoken:w5 S smokes now"
label values s5smoken smokes


***drop wave 5 Behavioural Risk file raw variables
drop `health_w5_br' 

***drop wave 5 Physical Health file raw variables
drop `health_w5_ph'

***drop wave 5 Generated Variables (Health) file raw variables
drop `health_w5_gv_health'

***drop wave 5 financial derived file raw variables***
drop `health_w5_imput'

******************************************************************************************


*set wave number
local wv = 6
local pre_wv = `wv' - 1
local wvs_before 5 4 2 1

***merge with wave 6 Behavioural Risk data***
local health_w6_br br015_ br016_ br001_ br002_ br039_ br040_ 
merge 1:1 mergeid using "$wave_6_br", keepusing(`health_w6_br') nogen

***merge with wave 6 Physical Health data***
local health_w6_ph ph003_ ph005_ ph049d2 ph049d1 ph049d3 ///
					ph049d4 ph049d5 ph049d6 ph049d7 ph049d10 ph049d13 ph049d11 ph049d12 ///
					ph049d9 ph049d8 ph048d1 ph048d2 ph048d3 ph048d4 ph048d5 ph048d6 ///
					ph048d9 ph048d10 ph048d7 ph048d8 ph006d2 ph006d3 ph006d5 ph006d10 ///
					ph006d6 ph006d1 ph006d4 ph013_ ph012_ ph006d11 ph006d12 ph006d13 ph006d14 ///
					ph006d16 ph006d18 ph006d19 ph006d20 ph072_?
merge 1:1 mergeid using "$wave_6_ph", keepusing(`health_w6_ph') nogen

***merge with wave 6 Generated Variables (Health) data***
local health_w6_gv_health euro1 euro2 euro3 euro4 euro5 euro6 euro7 ///
                          euro8 euro9 euro10 euro11 euro12
merge 1:1 mergeid using "$wave_6_gv_health", keepusing(`health_w6_gv_health') nogen

***merge with wave 6 imputation data***
local health_w6_imput eurod eurod_f
merge 1:1 mergeid using "$wave_6_imputations_", keepusing(`health_w6_imput') nogen

***merge with wave 6 technical variables***
local health_w6_techvar mn101_
merge 1:1 mergeid using "$wave_6_techvar", keepusing(`health_w6_techvar') nogen


***self-report of health***
*wave 6 respondent self-report of health
gen r6shlt =.
missing_common ph003_, result(r6shlt)
replace r6shlt = 1 if ph003_==1
replace r6shlt = 2 if ph003_==2
replace r6shlt = 3 if ph003_==3
replace r6shlt = 4 if ph003_==4
replace r6shlt = 5 if ph003_==5
label variable r6shlt "r6shlt:w6 r self-report of health"
label values r6shlt health

*wave 6 spouse self-report of health
gen s6shlt =.
spouse r6shlt, result(s6shlt) wave(6)
label variable s6shlt "s6shlt:w6 s self-report of health"
label values s6shlt health

***Whether health limits activities***
*wave 6 respondent whether health limits activites
gen r6hlthlma =.
missing_common ph005_, result(r6hlthlma)
replace r6hlthlma = 0 if ph005_ == 3
replace r6hlthlma = 1 if inrange(ph005_,1,2)
label variable r6hlthlma "r6hlthlma:w6 r hlth problem limits activities"
label values r6hlthlma limits

*wave 6 spouse whether health limits activities
gen s6hlthlma =.
spouse r6hlthlma, result(s6hlthlma) wave(6)
label variable s6hlthlma "s6hlthlma:w6 s hlth problem limits activities"
label values s6hlthlma limits

***Some difficulty walking cross room***
*wave 6 respondent difficulty walking across room
gen r6walkra =.
missing_common ph049d2, result(r6walkra)	
replace r6walkra = 0 if ph049d2==0
replace r6walkra = 1 if ph049d2==1
label variable r6walkra "r6walkra:w6 R Some Diff-Walk across room"
label values r6walkra adldiff

*wave 6 spouse difficulty walking across room
gen s6walkra =.
spouse r6walkra, result(s6walkra) wave(6)
label variable s6walkra "s6walkra:w6 S Some Diff-Walk across room"
label values s6walkra adldiff

***Some difficulty dressing***
*wave 6 respondent difficulty dressing
gen r6dressa =.
missing_common ph049d1, result(r6dressa)		
replace r6dressa = 0 if ph049d1==0
replace r6dressa = 1 if ph049d1==1
label variable r6dressa "r6dressa:w6 R Some Diff-Dressing"
label values r6dressa adldiff

*wave 6 spouse difficulty dressing
gen s6dressa =.
spouse r6dressa, result(s6dressa) wave(6)
label variable s6dressa "s6dressa:w6 S Some Diff-Dressing"
label values s6dressa adldiff

***Some difficulty bathing, shower***
*wave 6 respondent difficulty bathing, shower
gen r6batha =.
missing_common ph049d3, result(r6batha)		
replace r6batha = 0 if ph049d3==0
replace r6batha = 1 if ph049d3==1
label variable r6batha "r6batha:w6 R Some Diff-Bathing, shower"
label values r6batha adldiff

*wave 6 spouse difficulty bathing, shower
gen s6batha =.
spouse r6batha, result(s6batha) wave(6)
label variable s6batha "s6batha:w6 S Some Diff-Bathing, shower"
label values s6batha adldiff

***Some difficulty eating***
*wave 6 respondent difficulty eating
gen r6eata =.
missing_common ph049d4, result(r6eata)
replace r6eata = 0 if ph049d4==0
replace r6eata = 1 if ph049d4==1
label variable r6eata "r6eata:w6 R Some Diff-Eating"
label values r6eata adldiff

*wave 6 spouse difficulty eating
gen s6eata =.
spouse r6eata, result(s6eata) wave(6)
label variable s6eata "s6eata:w6 S Some Diff-Eating"
label values s6eata adldiff

***Some difficulty getting in/out of bed***
*wave 6 respondent difficulty getting in/out of bed
gen r6beda =.
missing_common ph049d5, result(r6beda)
replace r6beda = 0 if ph049d5==0
replace r6beda = 1 if ph049d5==1
label variable r6beda "r6beda:w6 R Some Diff-Get in/out bed"
label values r6beda adldiff

*wave 6 spouse difficulty getting in/out of bed
gen s6beda =.
spouse r6beda, result(s6beda) wave(6)
label variable s6beda "s6beda:w6 S Some Diff-Get in/out bed"
label values s6beda adldiff

***Some difficulty using the toilet***
*wave 6 respondent difficulty using the toilet
gen r6toilta =.
missing_common ph049d6, result(r6toilta)			
replace r6toilta = 0 if ph049d6==0
replace r6toilta = 1 if ph049d6==1
label variable r6toilta "r6toilta:w6 R Some Diff-Using the toilet"
label values r6toilta adldiff

*wave 6 spouse difficulty using the toilet
gen s6toilta =.
spouse r6toilta, result(s6toilta) wave(6)
label variable s6toilta "s6toilta:w6 S Some Diff-Using the toilet"
label values s6toilta adldiff

***Some difficulty using using a map***
*wave 6 respondent difficulty using a map
gen r6mapa =.
missing_common ph049d7, result(r6mapa)	
replace r6mapa = 0 if ph049d7==0
replace r6mapa = 1 if ph049d7==1
label variable r6mapa "r6mapa:w6 R Some Diff-Use a map"
label values r6mapa adldiff

*wave 6 spouse difficulty using a map
gen s6mapa =.
spouse r6mapa, result(s6mapa) wave(6)
label variable s6mapa "s6mapa:w6 S Some Diff-Use a map"
label values s6mapa adldiff

***Some difficulty using a telephone***
*wave 6 respondent difficulty using a telephone
gen r6phonea =.
missing_common ph049d10, result(r6phonea)			
replace r6phonea = 0 if ph049d10==0
replace r6phonea = 1 if ph049d10==1
label variable r6phonea "r6phonea:w6 R Some Diff-Use telephone"
label values r6phonea adldiff

*wave 6 spouse difficulty using a telephone
gen s6phonea =.
spouse r6phonea, result(s6phonea) wave(6)
label variable s6phonea "s6phonea:w6 S Some Diff-Use a telephone"
label values s6phonea adldiff

***Some difficulty managing money***
*wave 6 respondent difficulty managing money
gen r6moneya =.
missing_common ph049d13, result(r6moneya)				
replace r6moneya = 0 if ph049d13==0
replace r6moneya = 1 if ph049d13==1
label variable r6moneya "r6moneya:w6 R Some Diff-Managing money"
label values r6moneya adldiff

*wave 6 spouse difficulty managing money
gen s6moneya =.
spouse r6moneya, result(s6moneya) wave(6)
label variable s6moneya "s6moneya:w6 S Some Diff-Managing money"
label values s6moneya adldiff

***Some difficulty taking medications***
*wave 6 respondent difficulty taking medications
gen r6medsa =.
missing_common ph049d11, result(r6medsa)					
replace r6medsa = 0 if ph049d11==0
replace r6medsa = 1 if ph049d11==1
label variable r6medsa "r6medsa:w6 R Some Diff-Take medications"
label values r6medsa adldiff

*wave 6 spouse difficulty taking medications
gen s6medsa =.
spouse r6medsa, result(s6medsa) wave(6)
label variable s6medsa "s6medsa:w6 S Some Diff-Take medications"
label values s6medsa adldiff

***Some difficulty shopping for groceries***
*wave 6 respondent difficulty shopping for groceries
gen r6shopa =.
missing_common ph049d9, result(r6shopa)			
replace r6shopa = 0 if ph049d9==0
replace r6shopa = 1 if ph049d9==1
label variable r6shopa "r6shopa:w6 R Some Diff-Shop for grocery"
label values r6shopa adldiff

*wave 1 spouse difficulty shopping for groceries
gen s6shopa =.
spouse r6shopa, result(s6shopa) wave(6)
label variable s6shopa "s6shopa:w6 S Some Diff-Shop for grocery"
label values s6shopa adldiff

***Some difficulty preparing a hot meal***
*wave 6 respondent difficulty preparing a hot meal
gen r6mealsa =.
missing_common ph049d8, result(r6mealsa)
replace r6mealsa = 0 if ph049d8==0
replace r6mealsa = 1 if ph049d8==1
label variable r6mealsa "r6mealsa:w6 R Some Diff-Prepare hot meal"
label values r6mealsa adldiff

*wave 6 spouse difficulty preparing a hot meal
gen s6mealsa =.
spouse r6mealsa, result(s6mealsa) wave(6)
label variable s6mealsa "s6mealsa:w6 S Some Diff-Prepare hot meal"
label values s6mealsa adldiff

***Some difficulty doing work around the house***
*wave 6 respondent difficulty doing work around the house
gen r6housewka = .
missing_common ph049d12, result(r6housewka)
replace r6housewka = 0 if ph049d12==0
replace r6housewka = 1 if ph049d12==1
label variable r6housewka "r6housewka:w6 R some diff-doing work around house"
label values r6housewka adldiff

*wave 6 spouse difficulty doing work around the house
gen s6housewka = .
spouse r6housewka, result(s6housewka) wave(6)
label variable s6housewka "s6housewka:w6 s some diff-doing work around house"
label values s6housewka adldiff

***Difficulty Walking 100m***
*wave 6 respondent difficulty walking 100m
gen r6walk100a =.
missing_common ph048d1, result(r6walk100a)
replace r6walk100a = 0 if ph048d1==0
replace r6walk100a = 1 if ph048d1==1
label variable r6walk100a "r6walk100a:w6 R Some Diff-Walk 100m"
label values r6walk100a adldiff

*wave 6 spouse difficulty walking 100m
gen s6walk100a =.
spouse r6walk100a, result(s6walk100a) wave(6)
label variable s6walk100a "s6walk100a:w6 S Some Diff-Walk 100m"
label values s6walk100a adldiff

***Difficulty sitting for 2 hours***
*wave 6 respondent difficulty sitting for 2 hours
gen r6sita =.
missing_common ph048d2, result(r6sita)
replace r6sita = 0 if ph048d2==0
replace r6sita = 1 if ph048d2==1
label variable r6sita "r6sita:w6 R Some Diff-Sit for 2 hours"
label values r6sita adldiff

*wave 1 spouse difficulty sitting for 2 hours
gen s6sita =.
spouse r6sita, result(s6sita) wave(6)
label variable s6sita "s6sita:w6 S Some Diff-Sit for 2 hours"
label values s6sita adldiff

***Difficulty getting up from a chair***
*wave 6 respondent difficulty getting up from a chair
gen r6chaira =.
missing_common ph048d3 , result(r6chaira)	
replace r6chaira = 0 if ph048d3==0
replace r6chaira = 1 if ph048d3==1
label variable r6chaira "r6chaira:w6 R Some Diff-Get up fr chair"
label values r6chaira adldiff

*wave 6 spouse difficulty getting up from a chair
gen s6chaira =.
spouse r6chaira, result(s6chaira) wave(6)
label variable s6chaira "s6chaira:w6 S Some Diff-Get up fr chair"
label values s6chaira adldiff

***Difficulty climbing several flights of stairs***
*wave 6 respondent difficulty climbing several flights of stairs
gen r6climsa =.
missing_common ph048d4, result(r6climsa)
replace r6climsa = 0 if ph048d4==0
replace r6climsa = 1 if ph048d4==1
label variable r6climsa "r6climsa:w6 R Some Diff-Clmb sev flt str"
label values r6climsa adldiff

*wave 6 spouse difficulty climbing several flights of stairs
gen s6climsa =.
spouse r6climsa, result(s6climsa) wave(6)
label variable s6climsa "s6climsa:w6 S Some Diff-Clmb sev flt str"
label values s6climsa adldiff

***Difficulty climbing one flight of stairs***
*wave 6 respondent difficulty climbing one flight of stairs
gen r6clim1a =.
missing_common ph048d5, result(r6clim1a)		
replace r6clim1a = 0 if ph048d5==0
replace r6clim1a = 1 if ph048d5==1
label variable r6clim1a "r6clim1a:w6 R Some Diff-Clmb 1 flt str"
label values r6clim1a adldiff

*wave 6 spouse difficulty climbing one flight of stairs
gen s6clim1a =.
spouse r6clim1a, result(s6clim1a) wave(6)
label variable s6clim1a "s6clim1a:w6 S Some Diff-Clmb 1 flt str"
label values s6clim1a adldiff

***Difficulty stooping/kneeling/crouching***
*wave 6 respondent difficulty stooping/kneeling/crouching
gen r6stoopa =.
missing_common ph048d6, result(r6stoopa)		
replace r6stoopa = 0 if ph048d6==0
replace r6stoopa = 1 if ph048d6==1
label variable r6stoopa "r6stoopa:w6 R Some Diff-Stoop/Kneel/Crch"
label values r6stoopa adldiff

*wave 6 spouse difficulty stooping/kneeling/crouching
gen s6stoopa =.
spouse r6stoopa, result(s6stoopa) wave(6)
label variable s6stoopa "s6stoopa:w6 S Some Diff-Stoop/Kneel/Crch"
label values s6stoopa adldiff

***Difficulty lifting/carrying 10lbs***
*wave 6 respondent difficulty lifting/carrying 10lbs
gen r6lifta =.
missing_common ph048d9, result(r6lifta)	
replace r6lifta = 0 if ph048d9==0
replace r6lifta = 1 if ph048d9==1
label variable r6lifta "r6lifta:w6 R Some Diff-Lift/carry 10lbs"
label values r6lifta adldiff

*wave 6 spouse difficulty lifting/carrying 10lbs
gen s6lifta =.
spouse r6lifta, result(s6lifta) wave(6)
label variable s6lifta "s6lifta:w6 S Some Diff-Lift/carry 10lbs"
label values s6lifta adldiff

***Difficulty picking up a small coin***
*wave 6 respondent difficulty picking up a small coin
gen r6dimea =.
missing_common ph048d10, result(r6dimea)
replace r6dimea = 0 if ph048d10==0
replace r6dimea = 1 if ph048d10==1
label variable r6dimea "r6dimea:w6 R Some Diff-Pick up a small coin"
label values r6dimea adldiff

*wave 6 spouse difficulty picking up a small coin
gen s6dimea =.
spouse r6dimea, result(s6dimea) wave(6)
label variable s6dimea "s6dimea:w6 S Some Diff-Pick up a small coin"
label values s6dimea adldiff

***Difficulty reaching/extending arms up***
*wave 6 respondent difficulty reaching/extending arms up
gen r6armsa =.
missing_common ph048d7, result(r6armsa)
replace r6armsa = 0 if ph048d7==0
replace r6armsa = 1 if ph048d7==1
label variable r6armsa "r6armsa:w6 R Some Diff-Rch/xtnd arms up"
label values r6armsa adldiff

*wave 6 spouse difficulty reaching/extending arms up
gen s6armsa =.
spouse r6armsa, result(s6armsa) wave(6)
label variable s6armsa "s6armsa:w6 S Some Diff-Rch/xtnd arms up"
label values s6armsa adldiff

***Difficulty pushing/pulling a large object***
*wave 6 respondent difficulty pushing/pulling a large object
gen r6pusha =.
missing_common ph048d8, result(r6pusha)
replace r6pusha = 0 if ph048d8==0
replace r6pusha = 1 if ph048d8==1
label variable r6pusha "r6pusha:w6 R Some Diff-Push/pull lg obj"
label values r6pusha adldiff

*wave 6 spouse difficulty pushing/pulling a large object
gen s6pusha =.
spouse r6pusha, result(s6pusha) wave(6)
label variable s6pusha "s6pusha:w6 S Some Diff-Push/pull lg obj"
label values s6pusha adldiff

***ADL Summary 0-6***
*Wave 6 respondent ADL number of missings
egen r6adlam = rowmiss(r6batha r6dressa r6eata r6beda r6walkra) if inw6 == 1
label variable r6adlam "r6adlam:w6 Missings in ADL summary"

*wave 6 spouse ADL number of missings
gen s6adlam =.
spouse r6adlam, result(s6adlam) wave(6)
label variable s6adlam "s6adlam:w6 Missings in ADL summary"

local adl
foreach v of varlist r6batha r6dressa r6eata r6beda r6walkra {
	recode `v' (miss = 0), generate(`v'_m)
	local adl `adl' `v'_m
}

*Wave 6 respondent ADL Summary 0-5
gen r6adla =.
missing_h r6batha r6dressa r6eata r6beda r6walkra, result(r6adla)
replace r6adla = r6batha_m + r6dressa_m + r6eata_m + r6beda_m + r6walkra_m if inrange(r6adlam,0,4)
label variable r6adla "r6adla:w6 R Some Diff-ADLs /0-5"

*wave 6 spouse ADL Summary 0-5
gen s6adla =.
spouse r6adla, result(s6adla) wave(6)
label variable s6adla "s6adla:w6 S Some Diff-ADLs /0-5"

*drop intermediate variables
drop `adl'

***ADL Wallace Summary 0-3***
*Wave 6 respondent ADL Wallace number of missings
egen r6adlwam = rowmiss(r6batha r6dressa r6eata) if inw6 == 1
label variable r6adlwam "r6adlwam:w6 r Missings in ADL Wallace summary"

*wave 6 spouse ADL Wallace number of missings
gen s6adlwam =.
spouse r6adlwam, result(s6adlwam) wave(6)
label variable s6adlwam "s6adlwam:w6 s Missings in ADL Wallace summary"

local adlw
foreach v of varlist r6batha r6dressa r6eata {
	recode `v' (miss = 0), generate(`v'_m)
	local adlw `adlw' `v'_m
}

*Wave 6 respondent ADL Wallace Summary 0-3
gen r6adlwa =.
missing_h r6batha r6dressa r6eata, result(r6adlwa)
replace r6adlwa = r6batha_m + r6dressa_m + r6eata_m if inrange(r6adlwam,0,2)
label variable r6adlwa "r6adlwa:w6 R Some Diff-ADLs:Wallace /0-3"

*wave 6 spouse ADL Wallace Summary 0-3
gen s6adlwa =.
spouse r6adlwa, result(s6adlwa) wave(6)
label variable s6adlwa "s6adlwa:w6 S Some Diff-ADLs:Wallace /0-3"

*drop intermediate variables
drop `adlw'

***IADL Summary 0-3***
*Wave 6 respondent IADL number of missings
egen r6iadlam = rowmiss(r6phonea r6moneya r6medsa) if inw6 == 1
label variable r6iadlam "r6iadlam:w6 r Missings in IADL 0-3 summary"

*wave 6 spouse IADL number of missings
gen s6iadlam =.
spouse r6iadlam, result(s6iadlam) wave(6)
label variable s6iadlam "s6iadlam:w6 s Missings in IADL 0-3 summary"

local iadl
foreach v of varlist r6phonea r6moneya r6medsa {
	recode `v' (miss = 0), generate(`v'_m)
	local iadl `iadl' `v'_m
}

*Wave 6 respondent IADLs Summary 0-3
gen r6iadla =.
missing_h r6phonea r6moneya r6medsa, result(r6iadla)
replace r6iadla = r6phonea_m + r6moneya_m + r6medsa_m if inrange(r6iadlam,0,2)
label variable r6iadla "r6iadla:w6 R Some Diff-IADLs: /0-3"

*wave 6 spouse IADLs Summary 0-3
gen s6iadla =.
spouse r6iadla, result(s6iadla) wave(6)
label variable s6iadla "s6iadla:w6 S Some Diff-IADLs: /0-3"

*drop intermediate variables
drop `iadl'

***IADL Summary 0-5***
*Wave 6 respondent IADL number of missings
egen r6iadlzam = rowmiss(r6phonea r6moneya r6medsa r6shopa r6mealsa) if inw6 == 1
label variable r6iadlzam "r6iadlzam:w6 r Missings in IADL 0-5 summary"

*wave 6 spouse IADL number of missings
gen s6iadlzam =.
spouse r6iadlzam, result(s6iadlzam) wave(6)
label variable s6iadlzam "s6iadlzam:w6 s Missings in IADL 0-5 summary"

local iadlz
foreach v of varlist r6phonea r6moneya r6medsa r6shopa r6mealsa {
	recode `v' (miss = 0), generate(`v'_m)
	local iadlz `iadlz' `v'_m
}

*Wave 6 respondent IADLs Summary 0-5
gen r6iadlza =.
missing_h r6phonea r6moneya r6medsa r6shopa r6mealsa, result(r6iadlza)
replace r6iadlza = r6phonea_m + r6moneya_m + r6medsa_m + r6shopa_m + r6mealsa_m if inrange(r6iadlzam,0,4)
label variable r6iadlza "r6iadlza:w6 R Some Diff-IADLs: /0-5"

*wave 6 spouse IADLs Summary 0-5
gen s6iadlza =.
spouse r6iadlza, result(s6iadlza) wave(6)
label variable s6iadlza "s6iadlza:w6 S Some Diff-IADLs: /0-5"

*drop intermediate variables
drop `iadlz'

***Mobility Summary***
*Wave 6 respondent Mobility Summary number of missings
egen r6mobilbm = rowmiss(r6walk100a r6walkra r6climsa r6clim1a) if inw6 == 1
label variable r6mobilbm "r6mobilbm:w6 r Missings in mobility summary"

*wave 6 spouse Mobility Summary number of missings
gen s6mobilbm =.
spouse r6mobilbm, result(s6mobilbm) wave(6)
label variable s6mobilbm "s6mobilbm:w6 s Missings in mobility summary"

local mobilb
foreach v of varlist r6walk100a r6walkra r6climsa r6clim1a {
	recode `v' (miss = 0), generate(`v'_m)
	local mobilb `mobilb' `v'_m
}

*Wave 6 respondent Mobility Summary
gen r6mobilb =.
missing_h r6walk100a r6walkra r6climsa r6clim1a, result(r6mobilb)
replace r6mobilb = r6walk100a_m + r6walkra_m + r6climsa_m + r6clim1a_m if inrange(r6mobilbm,0,3)
label variable r6mobilb "r6mobilb:w6 R Some Diff-Mobility /0-4"

*wave 6 spouse Mobility Summary
gen s6mobilb =.
spouse r6mobilb, result(s6mobilb) wave(6)
label variable s6mobilb "s6mobilb:w6 S Some Diff-Mobility /0-4"

*drop intermediate variables
drop `mobilb'

***Large Muscle Summary***
*Wave 6 respondent Large Muscle Summary number of missings
egen r6lgmusam = rowmiss(r6sita r6chaira r6stoopa r6pusha) if inw6 == 1
label variable r6lgmusam "r6lgmusam:w6 r Missings in large muscle summary"

*wave 6 spouse Large Muscle Summary number of missings
gen s6lgmusam =.
spouse r6lgmusam, result(s6lgmusam) wave(6)
label variable s6lgmusam "s6lgmusam:w6 s Missings in large muscle summary"

local lgmusa
foreach v of varlist r6sita r6chaira r6stoopa r6pusha {
	recode `v' (miss = 0), generate(`v'_m)
	local lgmusa `lgmusa' `v'_m
}

*Wave 6 respondent Large Muscle Summary
gen r6lgmusa =.
missing_h r6sita r6chaira r6stoopa r6pusha, result(r6lgmusa)
replace r6lgmusa = r6sita_m + r6chaira_m + r6stoopa_m + r6pusha_m if inrange(r6lgmusam,0,3)
label variable r6lgmusa "r6lgmusa:w6 R Some Diff-Large muscle /0-4"

*wave 6 spouse Large Muscle Summary
gen s6lgmusa =.
spouse r6lgmusa, result(s6lgmusa) wave(6)
label variable s6lgmusa "s6lgmusa:w6 S Some Diff-Large muscle /0-4"

*drop intermediate variables
drop `lgmusa'

***Gross Motor Summary***
*Wave 6 respondent Gross Motor Summary number of missings
egen r6grossam = rowmiss(r6walk100a r6walkra r6clim1a r6beda r6batha) if inw6 == 1
label variable r6grossam "r6grossam:w6 r Missings in wk,rn,clmb,bd,bth summary"

*wave 6 spouse Gross Motor Summary number of missings
gen s6grossam =.
spouse r6grossam, result(s6grossam) wave(6)
label variable s6grossam "s6grossam:w6 s Missings in wk,rn,clmb,bd,bth summary"

local grossa
foreach v of varlist r6walk100a r6walkra r6clim1a r6beda r6batha {
	recode `v' (miss = 0), generate(`v'_m)
	local grossa `grossa' `v'_m
}

*Wave 6 respondent Gross Motor Summary
gen r6grossa =.
missing_h r6walk100a r6walkra r6clim1a r6beda r6batha, result(r6grossa)
replace r6grossa = r6walk100a_m + r6walkra_m + r6clim1a_m + r6beda_m + r6batha_m if inrange(r6grossam,0,4)
label variable r6grossa "r6grossa:w6 R Some Diff-Wk,rn,clmb,bd,bth /0-5"

*wave 6 spouse Gross Motor Summary
gen s6grossa =.
spouse r6grossa, result(s6grossa) wave(6)
label variable s6grossa "s6grossa:w6 S Some Diff-Wk,rn,clmb,bd,bth /0-5"

*drop intermediate variables
drop `grossa'

***Fine Motor Summary***
*Wave 6 respondent Fine Motor Summary number of missings
egen r6fineam = rowmiss(r6dimea r6eata r6dressa) if inw6 == 1
label variable r6fineam "r6fineam:w6 r Missings in dime,eat,dress summary"

*wave 6 spouse Gross Fine Summary number of missings
gen s6fineam =.
spouse r6fineam, result(s6fineam) wave(6)
label variable s6fineam "s6fineam:w6 s Missings in dime,eat,dress summary"

local finea
foreach v of varlist r6dimea r6eata r6dressa {
	recode `v' (miss = 0), generate(`v'_m)
	local finea `finea' `v'_m
}

*Wave 6 respondent Fine Motor Summary
gen r6finea =.
missing_h r6dimea r6eata r6dressa, result(r6finea)
replace r6finea = r6dimea_m + r6eata_m + r6dressa_m if inrange(r6fineam,0,2)
label variable r6finea "r6finea:w6 R Some Diff-dime,eat,dress /0-3"

*wave 6 spouse Fine Motor Summary
gen s6finea =.
spouse r6finea, result(s6finea) wave(6)
label variable s6finea "s6finea:w6 S Some Diff-dime,eat,dress /0-3"

*drop intermediate variables
drop `finea'

***EURO-D: Depression***
*wave 6 respondent depression
gen r6depress =.
missing_common euro1, result(r6depress)
replace r6depress = euro1 if inlist(euro1,0,1)
replace r6depress = .p if r6depress==.m & inlist(r6proxy,1,2)
label variable r6depress "r6depress:w6 EURO-D: r Depression (0,1)"
label values r6depress heurod

*wave 6 spouse depression
gen s6depress =.
spouse r6depress, result(s6depress) wave(6)
label variable s6depress "s6depress:w6 EURO-D: s Depression (0,1)"
label values s6depress heurod

***EURO-D: Pessimism***
*wave 6 respondent pessimism
gen r6pessim =.
missing_common euro2, result(r6pessim)
replace r6pessim = euro2 if inlist(euro2,0,1)
replace r6pessim = .p if r6pessim==.m & inlist(r6proxy,1,2)
label variable r6pessim "r6pessim:w6 EURO-D: r Pessimism (0,1)"
label values r6pessim heurod

*wave 6 spouse pessimism
gen s6pessim =.
spouse r6pessim, result(s6pessim) wave(6)
label variable s6pessim "s6pessim:w6 EURO-D: s Pessimism (0,1)"
label values s6pessim heurod

***EURO-D: Suicidality***
*wave 6 respondent suicidality
gen r6suicid =.
missing_common euro3, result(r6suicid)
replace r6suicid = euro3 if inlist(euro3,0,1)
replace r6suicid = .p if r6suicid==.m & inlist(r6proxy,1,2)
label variable r6suicid "r6suicid:w6 EURO-D: r Suicidality (0,1)"
label values r6suicid heurod

*wave 6 spouse suicidality
gen s6suicid =.
spouse r6suicid, result(s6suicid) wave(6)
label variable s6suicid "s6suicid:w6 EURO-D: s Suicidality (0,1)"
label values s6suicid heurod

***EURO-D: Guilt***
*wave 6 respondent guilt
gen r6guilt =.
missing_common euro4, result(r6guilt)
replace r6guilt = euro4 if inlist(euro4,0,1)
replace r6guilt = .p if r6guilt==.m & inlist(r6proxy,1,2)
label variable r6guilt "r6guilt:w6 EURO-D: r Guilt (0,1)"
label values r6guilt heurod

*wave 6 spouse guilt
gen s6guilt =.
spouse r6guilt, result(s6guilt) wave(6)
label variable s6guilt "s6guilt:w6 EURO-D: s Guilt (0,1)"
label values s6guilt heurod

***EURO-D: Sleep***
*wave 6 respondent sleep
gen r6sleep =.
missing_common euro5, result(r6sleep)
replace r6sleep = euro5 if inlist(euro5,0,1)
replace r6sleep = .p if r6sleep==.m & inlist(r6proxy,1,2)
label variable r6sleep "r6sleep:w6 EURO-D: r Sleep (0,1)"
label values r6sleep heurod

*wave 6 spouse sleep
gen s6sleep =.
spouse r6sleep, result(s6sleep) wave(6)
label variable s6sleep "s6sleep:w6 EURO-D: s Sleep (0,1)"
label values s6sleep heurod

***EURO-D: Interest***
*wave 6 respondent interest
gen r6intrst =.
missing_common euro6, result(r6intrst)
replace r6intrst = euro6 if inlist(euro6,0,1)
replace r6intrst = .p if r6intrst==.m & inlist(r6proxy,1,2)
label variable r6intrst "r6intrst:w6 EURO-D: r Interest (0,1)"
label values r6intrst heurod

*wave 6 spouse interest
gen s6intrst =.
spouse r6intrst, result(s6intrst) wave(6)
label variable s6intrst "s6intrst:w6 EURO-D: s Interest (0,1)"
label values s6intrst heurod

***EURO-D: Irritability***
*wave 6 respondent irritability
gen r6irritb =.
missing_common euro7, result(r6irritb)
replace r6irritb = euro7 if inlist(euro7,0,1)
replace r6irritb = .p if r6irritb==.m & inlist(r6proxy,1,2)
label variable r6irritb "r6irritb:w6 EURO-D: r Irritability (0,1)"
label values r6irritb heurod

*wave 6 spouse irritability
gen s6irritb =.
spouse r6irritb, result(s6irritb) wave(6)
label variable s6irritb "s6irritb:w6 EURO-D: s Irritability (0,1)"
label values s6irritb heurod

***EURO-D: Appetite***
*wave 6 respondent appetite
gen r6appett =.
missing_common euro8, result(r6appett)
replace r6appett = euro8 if inlist(euro8,0,1)
replace r6appett = .p if r6appett==.m & inlist(r6proxy,1,2)
label variable r6appett "r6appett:w6 EURO-D: r Appetite (0,1)"
label values r6appett heurod

*wave 6 spouse appetite
gen s6appett =.
spouse r6appett, result(s6appett) wave(6)
label variable s6appett "s6appett:w6 EURO-D: s Appetite (0,1)"
label values s6appett heurod

***EURO-D: Fatigue***
*wave 6 respondent fatigue
gen r6fatig =.
missing_common euro9, result(r6fatig)
replace r6fatig = euro9 if inlist(euro9,0,1)
replace r6fatig = .p if r6fatig==.m & inlist(r6proxy,1,2)
label variable r6fatig "r6fatig:w6 EURO-D: r Fatigue (0,1)"
label values r6fatig heurod

*wave 6 spouse fatigue
gen s6fatig =.
spouse r6fatig, result(s6fatig) wave(6)
label variable s6fatig "s6fatig:w6 EURO-D: s Fatigue (0,1)"
label values s6fatig heurod

***EURO-D: Concentration***
*wave 6 respondent concentration
gen r6concnt =.
missing_common euro10, result(r6concnt)
replace r6concnt = euro10 if inlist(euro10,0,1)
replace r6concnt = .p if r6concnt==.m & inlist(r6proxy,1,2)
label variable r6concnt "r6concnt:w6 EURO-D: r Concentration (0,1)"
label values r6concnt heurod

*wave 6 spouse concentration
gen s6concnt =.
spouse r6concnt, result(s6concnt) wave(6)
label variable s6concnt "s6concnt:w6 EURO-D: s Concentration (0,1)"
label values s6concnt heurod

***EURO-D: Enjoyment***
*wave 6 respondent enjoyment
gen r6enjoym =.
missing_common euro11, result(r6enjoym)
replace r6enjoym = euro11 if inlist(euro11,0,1)
replace r6enjoym = .p if r6enjoym==.m & inlist(r6proxy,1,2)
label variable r6enjoym "r6enjoym:w6 EURO-D: r Enjoyment (0,1)"
label values r6enjoym heurod

*wave 6 spouse enjoyment
gen s6enjoym =.
spouse r6enjoym, result(s6enjoym) wave(6)
label variable s6enjoym "s6enjoym:w6 EURO-D: s Enjoyment (0,1)"
label values s6enjoym heurod

***EURO-D: Tearfulness***
*wave 6 respondent tearfulness
gen r6tearfl =.
missing_common euro12, result(r6tearfl)
replace r6tearfl = euro12 if inlist(euro12,0,1)
replace r6tearfl = .p if r6tearfl==.m & inlist(r6proxy,1,2)
label variable r6tearfl "r6tearfl:w6 EURO-D: r Tearfulness (0,1)"
label values r6tearfl heurod

*wave 6 spouse tearfulness
gen s6tearfl =.
spouse r6tearfl, result(s6tearfl) wave(6)
label variable s6tearfl "s6tearfl:w6 EURO-D: s Tearfulness (0,1)"
label values s6tearfl heurod

***EURO-D: Score***
*wave 6 respondent EURO-D score
gen r6eurod =.
replace r6eurod = eurod
replace r6eurod = .p if r6depress==.m & inlist(r6proxy,1,2)
label variable r6eurod "r6eurod:w6 EURO-D: r Score"

*wave 6 respondent flag for EURO-D score
w6_imputation_flag eurod_f, gen(r6feurod)
label variable r6feurod "r6feurod:w6 EURO-D Flag: r Score"
label values r6feurod healthimpflag

*wave 6 spouse EURO-D score
gen s6eurod =.
spouse r6eurod, result(s6eurod) wave(6)
label variable s6eurod "s6eurod:w6 EURO-D: s Score"

*wave 6 spouse flag for EURO-D score
gen s6feurod =.
spouse r6feurod, result(s6feurod) wave(6)
label variable s6feurod "s6feurod:w6 EURO-D Flag: s Score"
label values s6feurod healthimpflag

***Ever had high blood pressure***
*wave 6 respondent ever had high blood pressure
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'hibpe == 1
}

gen r`wv'hibpe =.
missing_common ph006d2, result(r`wv'hibpe)
replace r`wv'hibpe = 0 if ph006d2 == 0
replace r`wv'hibpe = 1 if (ph006d2 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'hibpe "r`wv'hibpe:w`wv' r ever had high blood pressure"
label values r`wv'hibpe health_problems

*wave 6 spouse ever had high blood pressure
gen s6hibpe =.
spouse r6hibpe, result(s6hibpe) wave(6)
label variable s6hibpe "s6hibpe:w6 s ever had high blood pressure"
label values s6hibpe health_problems

***Ever had diabetes***
*wave 6 respondent ever had diabetes
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'diabe == 1
}

gen r`wv'diabe =.
missing_common ph006d5, result(r`wv'diabe)
replace r`wv'diabe = 0 if ph006d5 == 0
replace r`wv'diabe = 1 if (ph006d5 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'diabe "r`wv'diabe:w`wv' r ever had diabetes"
label values r`wv'diabe health_problems

*wave 6 spouse ever had diabetes
gen s6diabe =.
spouse r6diabe, result(s6diabe) wave(6)
label variable s6diabe "s6diabe:w6 s ever had diabetes"
label values s6diabe health_problems

***Ever had cancer***
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'cancre == 1
}

*wave 6 respondent ever had cancer
gen r`wv'cancre =.
missing_common ph006d10, result(r`wv'cancre)
replace r`wv'cancre = 0 if ph006d10 == 0
replace r`wv'cancre = 1 if (ph006d10 == 1 | ph072_3 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'cancre "r`wv'cancre:w`wv' r ever had cancer"
label values r`wv'cancre health_problems

*wave 6 dispute flag not available

***Ever had lung disease***
*wave 6 respondent ever had lung disease
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'lunge == 1
}

gen r`wv'lunge =.
missing_common ph006d6, result(r`wv'lunge)
replace r`wv'lunge = 0 if ph006d6 == 0
replace r`wv'lunge = 1 if (ph006d6 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'lunge "r`wv'lunge:w`wv' r ever had lung disease"
label values r`wv'lunge health_problems

*wave 6 spouse ever had lung disease
gen s6lunge =.
spouse r6lunge, result(s6lunge) wave(6)
label variable s6lunge "s6lunge:w6 s ever had lung disease"
label values s6lunge health_problems

***Ever had heart problems***
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'hearte == 1
}

*wave 6 respondent ever had heart problems
gen r`wv'hearte =.
missing_common ph006d1, result(r`wv'hearte)
replace r`wv'hearte = 0 if ph006d1 == 0
replace r`wv'hearte = 1 if (ph006d1 == 1  | ph072_1 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'hearte "r`wv'hearte:w`wv' r ever had heart problems"
label values r`wv'hearte health_problems

*wave 6 dispute flag not available

***Ever had stroke***
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'stroke == 1
}

*wave 6 respondent ever had stroke
gen r`wv'stroke =.
missing_common ph006d4, result(r`wv'stroke)
replace r`wv'stroke = 0 if ph006d4 == 0
replace r`wv'stroke = 1 if (ph006d4 == 1 | ph072_2 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'stroke "r`wv'stroke:w`wv' r ever had stroke"
label values r`wv'stroke health_problems

*wave 6 dispute flag not available

***Ever had arthritis***
*wave 6 respondent ever had arthritis
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'arthre == 1
}

gen r`wv'arthre =.
missing_common ph006d19 ph006d20, result(r`wv'arthre)
replace r6arthre = 0 if ph006d19 == 0 & ph006d20 == 0
replace r6arthre = 1 if (ph006d19 == 1 | ph006d20 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'arthre "r`wv'arthre:w`wv' r ever had arthritis"
label values r`wv'arthre health_problems

*wave 6 spouse ever had arthritis
gen s6arthre =.
spouse r6arthre, result(s6arthre) wave(6)
label variable s6arthre "s6arthre:w6 s ever had arthritis"
label values s6arthre health_problems

***Ever had high cholesterol***
*wave 6 respondent ever had high cholesterol
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'hchole == 1
}

gen r`wv'hchole = .
missing_common ph006d3, result(r`wv'hchole)
replace r`wv'hchole = ph006d3 if inrange(ph006d3,0,1)
label variable r`wv'hchole "r`wv'hchole:w`wv' r ever had high cholesterol"
label values r`wv'hchole health_problems

*wave 6 spouse ever had high cholesterol
gen s`wv'hchole = .
spouse r`wv'hchole, result(s`wv'hchole) wave(`wv')
label variable s`wv'hchole "s`wv'hchole:w`wv' s ever had high cholesterol"
label values s`wv'hchole health_problems

***Ever had Parkinsons***
*wave 6 respondent ever had parkinsons
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'parkine == 1
}

gen r`wv'parkine = .
missing_common ph006d12, result(r`wv'parkine)
replace r`wv'parkine = ph006d12 if inrange(ph006d12,0,1)
label variable r`wv'parkine "r`wv'parkine:w`wv' r ever had parkinson disease"
label values r`wv'parkine health_problems

*wave 6 spouse ever had parkinsons
gen s`wv'parkine = .
spouse r`wv'parkine, result(s`wv'parkine) wave(`wv')
label variable s`wv'parkine "s`wv'parkine:w`wv' s ever had parkinson disease"
label values s`wv'parkine health_problems

***Ever had cataracts***
*wave 6 respondent ever had cataracts
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'catrcte == 1
}

gen r`wv'catrcte = .
missing_common ph006d13, result(r`wv'catrcte)
replace r`wv'catrcte = ph006d13 if inrange(ph006d13,0,1)
label variable r`wv'catrcte "r`wv'catrcte:w`wv' r ever had cataracts"
label values r`wv'catrcte health_problems

*wave 6 spouse ever had cataracts
gen s`wv'catrcte = .
spouse r`wv'catrcte, result(s`wv'catrcte) wave(`wv')
label variable s`wv'catrcte "s`wv'catrcte:w`wv' s ever had cataracts"
label values s`wv'catrcte health_problems

***Ever had hip or femoral fracture***
*wave 6 respondent ever had hip or femoral fracture
local or_reported_earlier
foreach w in `wvs_before' {
	local or_reported_earlier `or_reported_earlier' | r`w'hipfeme == 1
}

*wave 6 respondent ever had hip or femoral fracture
gen r`wv'hipfeme = .
missing_common ph006d14, result(r`wv'hipfeme)
replace r`wv'hipfeme = 0 if ph006d14 == 0
replace r`wv'hipfeme = 1 if (ph006d14 == 1 | ph072_4 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'hipfeme "r`wv'hipfeme:w`wv' r ever had hip/femoral fracture"
label values r`wv'hipfeme health_problems

*wave 6 dispute flag not available

***Ever had psychiatric disorder***
*wave 6 respondent ever had psych disorder
local or_reported_earlier
foreach w in 2 4 {
    local or_reported_earlier `or_reported_earlier' | r`w'psyche == 1
}

gen r6psyche = .
missing_common ph006d18, result(r6psyche)
replace r6psyche = 0 if ph006d18 == 0
replace r6psyche = 1 if (ph006d18 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r6psyche "r6psyche:w6 r ever had psych disorder"
label values r6psyche health_problems

*wave 6 spouse ever had psych disorder
gen s6psyche = .
spouse r6psyche, result(s6psyche) wave(6)
label variable s6psyche "s6psyche:w6 s ever had psych disorder"
label values s6psyche health_problems

***Ever had ulcer***
*wave 6 respondent ever had ulcer
local or_reported_earlier
foreach w in `wvs_before' {
    local or_reported_earlier `or_reported_earlier' | r`w'ulcer == 1
}

gen r`wv'ulcer =.
missing_common ph006d11, result(r`wv'ulcer)
replace r`wv'ulcer = 0 if ph006d11 == 0
replace r`wv'ulcer = 1 if (ph006d11 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'ulcer "r`wv'ulcer:w`wv' r ever had ulcer"
label values r`wv'ulcer health_problems

*wave 6 spouse ever had ulcer
gen s6ulcer =.
spouse r6ulcer, result(s6ulcer) wave(6)
label variable s6ulcer "s6ulcer:w6 s ever had ulcer"
label values s6ulcer health_problems

***Ever had Alzheimer‚Äôs disease, dementia***
*wave 6 respondent ever had Alzheimer‚Äôs disease, dementia
local or_reported_earlier
foreach w in 4 2 {
    local or_reported_earlier `or_reported_earlier' | r`w'alzdem == 1
}

gen r`wv'alzdem =.
missing_common ph006d16, result(r`wv'alzdem)
replace r`wv'alzdem = 0 if ph006d16 == 0
replace r`wv'alzdem = 1 if (ph006d16 == 1 `or_reported_earlier') & inw`wv' == 1
label variable r`wv'alzdem "r`wv'alzdem:w`wv' r ever had alzheimer's/dementia"
label values r`wv'alzdem health_problems

*wave 6 spouse ever had Alzheimer‚Äôs disease, dementia
gen s6alzdem =.
spouse r6alzdem, result(s6alzdem) wave(6)
label variable s6alzdem "s6alzdem:w6 s ever had alzheimer's/dementia"
label values s6alzdem health_problems

***Height in meters***
*wave 6 respondent height in meters
gen r6height =.
missing_common ph013_, result(r6height)
replace r6height = .i if inrange(ph013_,0,89)
replace r6height = ph013_ if inrange(ph013_,1.00,1.90) & inw6==1
replace r6height = ph013_/100 if inrange(ph013_,90,220) & inw6==1
label variable r6height "r6height:w6 r Height in meters"

*wave 6 spouse height in meters
gen s6height =.
spouse r6height, result(s6height) wave(6)
label variable s6height "s6height:w6 s Height in meters"

***Weight in kilograms***
*wave 6 respondent weight in kilograms
gen r6weight =.
missing_common ph012_, result(r6weight)
replace r6weight = .i if inrange(ph012_,0,12)
replace r6weight = ph012_ if inrange(ph012_,13,250)
label variable r6weight "r6weight:w6 r Weight in kilograms"

*wave 6 spouse weight in kilograms
gen s6weight =.
spouse r6weight, result(s6weight) wave(6)
label variable s6weight "s6weight:w6 s Weight in kilograms"

***BMI***
*wave 6 respondent BMI
gen r6bmi =.
missing_h r6weight r6height, result(r6bmi)
replace r6bmi = r6weight/(r6height^2) if !mi(r6weight) & !mi(r6height)
replace r6bmi = .i if (r6height==.i | r6weight==.i | !inrange(r6bmi,12,100)) & inw`wv' == 1
label variable r6bmi "r6bmi:w6 r Body Mass Index=kg/m2"

*wave 6 spouse BMI
gen s6bmi =.
spouse r6bmi, result(s6bmi) wave(6)
label variable s6bmi "s6bmi:w6 s Body Mass Index=kg/m2"

***BMI Categorization***
*wave 6 respondent BMI categorization
gen r6bmicat = .
missing_h r6weight r6height, result(r6bmicat)
replace r6bmicat = 1 if inrange(r6bmi,1,18.49999)
replace r6bmicat = 2 if inrange(r6bmi,18.50,24.99999)
replace r6bmicat = 3 if inrange(r6bmi,25,29.99999)
replace r6bmicat = 4 if inrange(r6bmi,30,34.99999)
replace r6bmicat = 5 if inrange(r6bmi,35,39.99999)
replace r6bmicat = 6 if inrange(r6bmi,40,100)
replace r6bmicat = .i if r6weight==.i | r6height==.i | r6bmi==.i
label variable r6bmicat "r6bmicat:w6 r BMI categorization"
label values r6bmicat bmicate

*wave 6 spouse BMI categorization
gen s6bmicat = .
spouse r6bmicat, result(s6bmicat) wave(6)
label variable s6bmicat "s6bmicat:w6 s BMI categorization"
label values s6bmicat bmicate

***Vigorous physical activity***
*wave 6 respondent vigorous physical activity
gen r6vgactx =.
missing_common br015_, result(r6vgactx)
replace r6vgactx = 2 if br015_ == 1
replace r6vgactx = 3 if br015_ == 2
replace r6vgactx = 4 if br015_ == 3
replace r6vgactx = 5 if br015_ == 4
label variable r6vgactx "r6vgactx:w6 R Freq vigorous phys activ"
label values r6vgactx activity

*wave 6 spouse vigorous physical activity
gen s6vgactx =.
spouse r6vgactx, result(s6vgactx) wave(6)
label variable s6vgactx "s6vgactx:w6 S Freq vigorous phys activ"
label values s6vgactx activity

***Moderate physical activity***
*wave 6 respondent moderate physical activity
gen r6mdactx =.
missing_common br016_, result(r6mdactx)
replace r6mdactx = 2 if br016_ == 1
replace r6mdactx = 3 if br016_ == 2
replace r6mdactx = 4 if br016_ == 3
replace r6mdactx = 5 if br016_ == 4
label variable r6mdactx "r6mdactx:w6 R Freq moderate phys activ"
label values r6mdactx activity

*wave 6 spouse vigorous moderate activity
gen s6mdactx =.
spouse r6mdactx, result(s6mdactx) wave(6)
label variable s6mdactx "s6mdactx:w6 S Freq moderate phys activ"
label values s6mdactx activity

***Ever drinks alcohol***
**change to last 7 days
*wave 6 respondent ever drinks alcohol
gen r6drink =.
missing_common br039_, result(r6drink)
replace r6drink = 0 if br039_ == 5
replace r6drink = 1 if br039_ == 1
label variable r6drink "r6drink:w6 R ever drinks any alcohol"
label values r6drink ever_drink

*wave 6 spouse ever drinks alcohol
gen s6drink =.
spouse r6drink, result(s6drink) wave(6)
label variable s6drink "s6drink:w6 S ever drinks any alcohol"
label values s6drink ever_drink

***No. Drinks in last 7 days***
*wave 6 respondent no. drinks in last 7 days
gen r6drinkn7 = .
missing_common br040_, result(r6drinkn7)
replace r6drinkn7 = br040_ if inrange(br040_,0,100)
replace r6drinkn7 = 0 if br039_ == 5
label variable r6drinkn7 "r6drinkn7:w6 r no. drinks in last 7 days"

*wave 6 spouse no. drinks in last 7 days
gen s6drinkn7 = .
spouse r6drinkn7, result(s6drinkn7) wave(6)
label variable s6drinkn7 "s6drinkn7:w6 s no. drinks in last 7 days"

***Ever smoked cigarettes***
*wave 6 respondent ever smoked cigarettes
gen r6smokev =.
missing_common br001_, result(r6smokev)
replace r6smokev = r1smokev if !mi(r1smokev) & inw6 == 1
replace r6smokev = r2smokev if !mi(r2smokev) & inw6 == 1
replace r6smokev = r4smokev if !mi(r4smokev) & inw6 == 1
replace r6smokev = r5smokev if !mi(r5smokev) & inw6 == 1
replace r6smokev = 0 if br001_ == 5
replace r6smokev = 1 if br001_ == 1 | br002_ == 1
label variable r6smokev "r6smokev:w6 R smoke ever"
label values r6smokev smokes

*wave 6 spouse ever smoked cigarettes
gen s6smokev =.
spouse r6smokev, result(s6smokev) wave(6)
label variable s6smokev "s6smokev:w6 S smoke ever"
label values s6smokev smokes

***Smokes cigarettes now***
*wave 6 respondent smokes cigarettes now
***define same way at wave 1
gen r6smoken =.
missing_common br001_ br002_, result(r6smoken)
replace r6smoken = .s if mn101_ == 1
replace r6smoken = 0 if br001_ == 5 | br002_ == 5
replace r6smoken = 1 if br002_ == 1
label variable r6smoken "r6smoken:w6 R smokes now"
label values r6smoken smokes

*wave 6 spouse smokes cigarettes now
gen s6smoken =.
spouse r6smoken, result(s6smoken) wave(6)
label variable s6smoken "s6smoken:w6 S smokes now"
label values s6smoken smokes


***drop wave 6 Behavioural Risk file raw variables
drop `health_w6_br'

***drop wave 6 Physical Health file raw variables
drop `health_w6_ph'

***drop wave 6 Generated Variables (Health) file raw variables
drop `health_w6_gv_health'

***drop wave 6 financial derived file raw variables***
drop `health_w6_imput'

***drop wave 6 technical variables***
drop `health_w6_techvar'

******************************************************************************************


*set wave number
local wvs 1 2 4 5 6
local prv_wvs 1 2 4 5


*Spouse ever have heart problem
foreach wv in `wvs' {
    gen s`wv'hearte =.
    spouse r`wv'hearte, result(s`wv'hearte) wave(`wv')
    label variable s`wv'hearte "s`wv'hearte:w`wv' s ever had heart problem"
    label values s`wv'hearte health_problems
}

*Spouse heart problem dispute flag
foreach wv in `prv_wvs' {
    gen s`wv'heartf =.
    spouse r`wv'heartf, result(s`wv'heartf) wave(`wv')
    label variable s`wv'heartf "s`wv'heartf:w`wv' s heart problem dispute flag"
    label values s`wv'heartf ehealth_problems_flag
}

*Spouse ever have cancer
foreach wv in `wvs' {
    gen s`wv'cancre =.
    spouse r`wv'cancre, result(s`wv'cancre) wave(`wv')
    label variable s`wv'cancre "s`wv'cancre:w`wv' s ever had cancer"
    label values s`wv'cancre health_problems
}

*Spouse cancer dispute flag
foreach wv in `prv_wvs' {
    gen s`wv'cancrf =.
    spouse r`wv'cancrf, result(s`wv'cancrf) wave(`wv')
    label variable s`wv'cancrf "s`wv'cancrf:w`wv' s cancer dispute flag"
    label values s`wv'cancrf ehealth_problems_flag
}

*Spouse ever have stroke
foreach wv in `wvs' {
    gen s`wv'stroke =.
    spouse r`wv'stroke, result(s`wv'stroke) wave(`wv')
    label variable s`wv'stroke "s`wv'stroke:w`wv' s ever had stroke"
    label values s`wv'stroke health_problems
}

*Spouse stroke dispute flag
foreach wv in `prv_wvs' {
    gen s`wv'strokf =.
    spouse r`wv'strokf, result(s`wv'strokf) wave(`wv')
    label variable s`wv'strokf "s`wv'strokf:w`wv' s stroke dispute flag"
    label values s`wv'strokf ehealth_problems_flag
}

*Spouse ever have hip/femoral fracture
foreach wv in `wvs' {
    gen s`wv'hipfeme =.
    spouse r`wv'hipfeme, result(s`wv'hipfeme) wave(`wv')
    label variable s`wv'hipfeme "s`wv'hipfeme:w`wv' s ever had hip/femoral fracture"
    label values s`wv'cancre health_problems
}

*Spouse hip/femoral fracture dispute flag
foreach wv in `prv_wvs' {
    gen s`wv'hipfemf =.
    spouse r`wv'hipfemf, result(s`wv'hipfemf) wave(`wv')
    label variable s`wv'hipfemf "s`wv'hipfemf:w`wv' s hip/femoral fracture dispute flag"
    label values s`wv'hipfemf ehealth_problems_flag
}

***Change in self-reported health***
*wave respondent change in self-reported health & flag
foreach w in 2 4 5 6 {
	gen preload =.
	if `w'==2 {
		replace preload = r1shlt if r1shlt != .
	}
	else if `w'==4 {
		replace preload = r2shlt if r2shlt != .
		replace preload = r1shlt if r1shlt != . & preload == .
	}
	else if `w'==5 {
	    replace preload = r4shlt if r4shlt != .
		replace preload = r2shlt if r2shlt != . & preload == .
		replace preload = r1shlt if r1shlt != . & preload == .
	}
	else if `w'==6 {
		replace preload = r5shlt if r5shlt != .
	    replace preload = r4shlt if r4shlt != . & preload == .
		replace preload = r2shlt if r2shlt != . & preload == .
		replace preload = r1shlt if r1shlt != . & preload == .
	}
	else {
	    di as error "must set preloads for new wave"
	    exit 198
	}

	gen r`w'shltc =.
	replace r`w'shltc = .c if preload == . & inw`w' == 1
	missing_h r`w'shlt preload if inw`w'==1, result(r`w'shltc)
	replace r`w'shltc = .p if r`w'shlt == .p
	replace r`w'shltc = r`w'shlt - preload if !mi(r`w'shlt) & !mi(preload)
	label variable r`w'shltc "r`w'shltc:w`w' r Calc change in self-reported hlth"

	gen r`w'shltcf =.
	replace r`w'shltcf = .m if r`w'shlt == .m
	replace r`w'shltcf = .d if r`w'shlt == .d
	replace r`w'shltcf = .r if r`w'shlt == .r
	replace r`w'shltcf = 9 if mi(preload) & !mi(r`w'shlt)
	if `w'==2 {
		replace r`w'shltcf = 0 if preload==r1shlt & r1shlt != . & !mi(r`w'shltc)
	}
	else if `w'==4 {
	    replace r`w'shltcf = 1 if preload==r2shlt & r2shlt != . & !mi(r`w'shltc)
	    replace r`w'shltcf = 0 if preload==r1shlt & r1shlt != . & !mi(r`w'shltc) & mi(r`w'shltcf)
	}
	else if `w'==5 {
	    replace r`w'shltcf = 2 if preload==r4shlt & r4shlt != . & !mi(r`w'shltc)
	    replace r`w'shltcf = 1 if preload==r2shlt & r2shlt != . & !mi(r`w'shltc) & mi(r`w'shltcf)
	    replace r`w'shltcf = 0 if preload==r1shlt & r1shlt != . & !mi(r`w'shltc) & mi(r`w'shltcf)
	}
	else if `w'==6 {
	    replace r`w'shltcf = 3 if preload==r5shlt & r5shlt != . & !mi(r`w'shltc)
	    replace r`w'shltcf = 2 if preload==r4shlt & r4shlt != . & !mi(r`w'shltc) & mi(r`w'shltcf)
	    replace r`w'shltcf = 1 if preload==r2shlt & r2shlt != . & !mi(r`w'shltc) & mi(r`w'shltcf)
	    replace r`w'shltcf = 0 if preload==r1shlt & r1shlt != . & !mi(r`w'shltc) & mi(r`w'shltcf)
	}
	else {
	    di as error "must set preloads for new wave"
	    exit 198
	}

	label variable r`w'shltcf "r`w'shltcf:w`w' r Flag calc chg self-reported hlth"
	label values r`w'shltcf changeflag

	
		
	drop preload
}

*wave spouse change in self-reported
foreach w in 2 4 5 6 {
	gen s`w'shltc =.
	spouse r`w'shltc, result(s`w'shltc) wave(`w')
	label variable s`w'shltc "s`w'shltc:w`w' s Calc change in self-reported hlth"
}

*wave spouse flag change in self-reported health
foreach w in 2 4 5 6 {
	gen s`w'shltcf =.
	spouse r`w'shltcf, result(s`w'shltcf) wave(`w')
	label variable s`w'shltcf "s`w'shltcf:w`w' s Flag calc chg self-reported hlth"
	label values s`w'shltcf changeflag
}


******************************************************************************************


*cognition date naming
label define recall ///
   0 "0.incorrect" ///
   1 "1.correct" ///
   .m ".m:Missing" ///
   .n ".n:not asked(re-IW)" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr" ///
   .p ".p:proxy ivw" 
   
*cognition factors
label define cogfac ///
	0 "0.no" ///
	1 "1.yes" ///
	.p ".p:proxy"

   
******************************************************************************************

   

***merge with wave 1 Cognitive Function data***
local cog_w1_cf cf003_ cf004_ cf005_ cf006_ cf010_ cf008tot cf016tot cf017 cf018d*
merge 1:1 mergeid using "$wave_1_cf", keepusing(`cog_w1_cf') nogen

***merge with wave 1 Generated Variables (Health) data***
local cog_w1_gv_health numeracy orienti
merge 1:1 mergeid using "$wave_1_gv_health", keepusing(`cog_w1_gv_health') nogen


***immediate word recall***
*wave 1 respondent immediate word recall
gen r1imrc =.
missing_common cf008tot, result(r1imrc)
replace r1imrc = cf008tot if inrange(cf008tot,0,10)
replace r1imrc = .p if r1imrc==.m & inlist(r1proxy,1,2)
label variable r1imrc "r1imrc:w1 r immediate word recall"

*wave 1 spouse immediate word recall
gen s1imrc =.
spouse r1imrc, result(s1imrc) wave(1)
label variable s1imrc "s1imrc:w1 s immediate word recall"

***delayed word recall***
*wave 1 respondent delayed word recall
gen r1dlrc =.
missing_common cf016tot, result(r1dlrc)
replace r1dlrc = cf016tot if inrange(cf016tot,0,10)
replace r1dlrc = .p if r1dlrc==.m & inlist(r1proxy,1,2)
label variable r1dlrc "r1dlrc:w1 r delayed word recall"

*wave 1 spouse delayed word recall
gen s1dlrc =.
spouse r1dlrc, result(s1dlrc) wave(1)
label variable s1dlrc "s1dlrc:w1 s delayed word recall"

***cognition month naming***
*wave 1 respondent cognition month naming
gen r1mo= .
missing_common cf004_, result(r1mo)
replace r1mo = 1 if cf004_==1 
replace r1mo = 0 if cf004_==2
label variable r1mo "r1mo:w1 r cognition date naming-month"
label values r1mo recall

*wave 1 spouse month naming
gen s1mo =.
spouse r1mo, result(s1mo) wave(1)
label variable s1mo "s1mo:w1 s cognition date naming-month"
label values s1mo recall

***cognition day naming***
*wave 1 respondent cognition day naming
gen r1dy= .
missing_common cf003_, result(r1dy)
replace r1dy = 1 if cf003_==1 
replace r1dy = 0 if cf003_==2
label variable r1dy "r1dy:w1 r cognition date naming-day of month"
label values r1dy recall

*wave 1 spouse day naming
gen s1dy =.
spouse r1dy, result(s1dy) wave(1)
label variable s1dy "s1dy:w1 s cognition date naming-day of month"
label values s1dy recall

***cognition year naming***
*wave 1 respondent cognition year naming
gen r1yr= .
missing_common cf005_, result(r1yr)
replace r1yr = 1 if cf005_==1 
replace r1yr = 0 if cf005_==2
label variable r1yr "r1yr:w1 r cognition date naming-year"
label values r1yr recall

*wave 1 spouse year naming
gen s1yr =.
spouse r1yr, result(s1yr) wave(1)
label variable s1yr "s1yr:w1 s cognition date naming-year"
label values s1yr recall

***cognition day of week naming***
*wave 1 respondent cognition day of week naming
gen r1dw= .
missing_common cf006_, result(r1dw)
replace r1dw = 1 if cf006_==1 
replace r1dw = 0 if cf006_==2

label variable r1dw "r1dw:w1 r cognition date naming-day of week"
label values r1dw recall

*wave 1 spouse day of week naming
gen s1dw =.
spouse r1dw, result(s1dw) wave(1)
label variable s1dw "s1dw:w1 s cognition date naming-day of week"
label values s1dw recall

***cognition orient***
*wave 1 respondent cognition orient
gen r1orient=.
replace r1orient=orienti if !mi(orienti)
replace r1orient = .m if (r1mo==.m | r1dy==.m | r1yr==.m | r1dw==.m) & mi(r1orient)
replace r1orient = .d if (r1mo==.d | r1dy==.d | r1yr==.d | r1dw==.d) & inlist(r1orient,.,.m)
replace r1orient = .r if (r1mo==.r | r1dy==.r | r1yr==.r | r1dw==.r) & inlist(r1orient,.,.m)
label variable r1orient "r1orient:w1 r cognition orient (summary date naming)"

*wave 1 spouse cognition orient
gen s1orient =.
spouse r1orient, result(s1orient) wave(1)
label variable s1orient "s1orient:w1 s cognition orient (summary date naming)"

***verbal fluency score***
*wave 1 respondent verbal fluency score
gen r1verbf= .
missing_common cf010_, result(r1verbf)
replace r1verb = cf010_ if inrange(cf010_,0,100)
replace r1verb = .p if r1verb==.m & inlist(r1proxy,1,2)
label variable r1verbf "r1verbf:w1 r verbal fluency score"

*wave 1 spouse verbal fluency score
gen s1verbf =.
spouse r1verbf, result(s1verbf) wave(1)
label variable s1verbf "s1verbf:w1 s verbal fluency score"

***numeracy  score***
*wave 1 respondent numeracy  score
gen r1numer_s= .
missing_common numeracy, result(r1numer_s)
replace r1numer_s = numeracy if inrange(numeracy,1,5)
label variable r1numer_s "r1numer_s:w1 r numeracy  score"

*wave 1 spouse numeracy  score
gen s1numer_s =.
spouse r1numer_s, result(s1numer_s) wave(1)
label variable s1numer_s "s1numer_s:w1 s numeracy  score"

***recall summary  score***
*wave 1 respondent recall summary  score
gen r1tr20 = r1imrc + r1dlrc 
missing_h r1imrc r1dlrc if r1tr20==., result(r1tr20)
replace r1tr20 = .p if r1imrc==.p & r1dlrc==.p
label variable r1tr20 "r1tr20:w1 r recall summary  score"

*wave 1 spouse recall summary  score
gen s1tr20 =.
spouse r1tr20, result(s1tr20) wave(1)
label variable s1tr20 "s1tr20:w1 s recall summary  score"

***Whether Factors Impaired Cognition Tests***
*wave 1 respondent whether factors impaired cognition tests
gen r1cogimp = .
missing_common cf017, result(r1cogimp)
replace r1cogimp = 0 if cf017 == 5
replace r1cogimp = 1 if cf017 == 1
label variable r1cogimp "r1cogimp:w1 r whether factors impaired cognition tests"
label values r1cogimp cogfac

*wave 1 spouse whether factors impaired cognition tests
gen s1cogimp = .
spouse r1cogimp, result(s1cogimp) wave(1)
label variable s1cogimp "s1cogimp:w1 s whether factors impaired cognition tests"
label values s1cogimp cogfac

***Whether Other People Present During Cog Tests***
*wave 1 respondent whether other people present during cog tests
gen r1cogothp = .
missing_common cf018d1 cf018d2 cf018d3 cf018d4, result(r1cogothp)
replace r1cogothp = 0 if cf018d1 == 1 | cf018d2 == 0 | cf018d3 == 0 | cf018d4 == 0
replace r1cogothp = 1 if cf018d1 == 0
label variable r1cogothp "r1cogothp:w1 r whether other people present during cog tests"
label values r1cogothp cogfac

*wave 1 spouse whether other people present during cog tests
gen s1cogothp = .
spouse r1cogothp, result(s1cogothp) wave(1)
label variable s1cogothp "s1cogothp:w1 s whether other people present during cog tests"
label values s1cogothp cogfac



***drop wave 1 Cognitive Function file raw variables
drop `cog_w1_cf'

***drop wave 1 Generated Variables (Health) file raw variables
drop `cog_w1_gv_health'

******************************************************************************************


***merge with wave 2 Cognitive Function data***
local cog_w2_cf cf003_ cf004_ cf005_ cf006_ cf010_ cf008tot cf016tot cf017_ cf018d*
merge 1:1 mergeid using "$wave_2_cf", keepusing(`cog_w2_cf') nogen 

***merge with wave 2 Generated Variables (Health) data***
local cog_w2_gv_health numeracy orienti
merge 1:1 mergeid using "$wave_2_gv_health", keepusing(`cog_w2_gv_health') nogen


***immediate word recall***
*wave 2 respondent immediate word recall
gen r2imrc =.
missing_common cf008tot, result(r2imrc)
replace r2imrc = cf008tot if inrange(cf008tot,0,10)
replace r2imrc = .p if r2imrc==.m & inlist(r2proxy,1,2)
label variable r2imrc "r2imrc:w2 r immediate word recall"

*wave 2 spouse immediate word recall
gen s2imrc =.
spouse r2imrc, result(s2imrc) wave(2)
label variable s2imrc "s2imrc:w2 s immediate word recall"

***delayed word recall***
*wave 2 respondent delayed word recall
gen r2dlrc =.
missing_common cf016tot, result(r2dlrc)
replace r2dlrc = cf016tot if inrange(cf016tot,0,10)
replace r2dlrc = .p if r2dlrc==.m & inlist(r2proxy,1,2)
label variable r2dlrc "r2dlrc:w2 r delayed word recall"

*wave 2 spouse delayed word recall
gen s2dlrc =.
spouse r2dlrc, result(s2dlrc) wave(2)
label variable s2dlrc "s2dlrc:w2 s delayed word recall"

***cognition month naming***
*wave 2 respondent cognition month naming
gen r2mo= .
missing_common cf004_, result(r2mo)
replace r2mo = 1 if cf004_==1 
replace r2mo = 0 if cf004_==2
label variable r2mo "r2mo:w2 r cognition date naming-month"
label values r2mo recall

*wave 2 spouse month naming
gen s2mo =.
spouse r2mo, result(s2mo) wave(2)
label variable s2mo "s2mo:w2 s cognition date naming-month"
label values s2mo recall

***cognition day naming***
*wave 2 respondent cognition day naming
gen r2dy= .
missing_common cf003_, result(r2dy)
replace r2dy = 1 if cf003_==1 
replace r2dy = 0 if cf003_==2
label variable r2dy "r2dy:w2 r cognition date naming-day of month"
label values r2dy recall

*wave 2 spouse day naming
gen s2dy =.
spouse r2dy, result(s2dy) wave(2)
label variable s2dy "s2dy:w2 s cognition date naming-day of month"
label values s2dy recall


***cognition year naming***
*wave 2 respondent cognition year naming
gen r2yr= .
missing_common cf005_, result(r2yr)
replace r2yr = 1 if cf005_==1 
replace r2yr = 0 if cf005_==2
label variable r2yr "r2yr:w2 r cognition date naming-year"
label values r2yr recall

*wave 2 spouse year naming
gen s2yr =.
spouse r2yr, result(s2yr) wave(2)
label variable s2yr "s2yr:w2 s cognition date naming-year"
label values s2yr recall

***cognition day of week naming***
*wave 2 respondent cognition day of week naming
gen r2dw= .
missing_common cf006_, result(r2dw)
replace r2dw = 1 if cf006_==1 
replace r2dw = 0 if cf006_==2
label variable r2dw "r2dw:w2 r cognition date naming-day of week"
label values r2dw recall

*wave 2 spouse day of week naming
gen s2dw =.
spouse r2dw, result(s2dw) wave(2)
label variable s2dw "s2dw:w2 s cognition date naming-day of week"
label values s2dw recall

***cognition orient***
*wave 2 respondent cognition orient
gen r2orient=.
replace r2orient=orienti if !mi(orienti)
replace r2orient = .m if (r2mo==.m | r2dy==.m | r2yr==.m | r2dw==.m) & mi(r2orient)
replace r2orient = .d if (r2mo==.d | r2dy==.d | r2yr==.d | r2dw==.d) & inlist(r2orient,.,.m)
replace r2orient = .r if (r2mo==.r | r2dy==.r | r2yr==.r | r2dw==.r) & inlist(r2orient,.,.m)
label variable r2orient "r2orient:w2 r cognition orient (summary date naming)"

*wave 2 spouse cognition orient
gen s2orient =.
spouse r2orient, result(s2orient) wave(2)
label variable s2orient "s2orient:w2 s cognition orient (summary date naming)"

***verbal fluency score***
*wave 2 respondent verbal fluency score
gen r2verbf= .
missing_common cf010_, result(r2verbf)
replace r2verb = cf010_ if inrange(cf010_,0,100)
replace r2verb = .p if r2verb==.m & inlist(r2proxy,1,2)
label variable r2verbf "r2verbf:w2 r verbal fluency score"

*wave 2 spouse verbal fluency score
gen s2verbf =.
spouse r2verbf, result(s2verbf) wave(2)
label variable s2verbf "s2verbf:w2 s verbal fluency score"

***numeracy  score***
*wave 2 respondent numeracy  score
gen r2numer_s= .
missing_common numeracy, result(r2numer_s)
replace r2numer_s = numeracy if inrange(numeracy,1,5)
label variable r2numer_s "r2numer_s:w2 r numeracy  score"

*wave 2 spouse numeracy  score
gen s2numer_s =.
spouse r2numer_s, result(s2numer_s) wave(2)
label variable s2numer_s "s2numer_s:w2 s numeracy  score"

***recall summary  score***
*wave 2 respondent recall summary  score
gen r2tr20 = r2imrc + r2dlrc 
missing_h r2imrc r2dlrc if r2tr20==., result(r2tr20)
replace r2tr20 = .p if r2imrc==.p & r2dlrc==.p
label variable r2tr20 "r2tr20:w2 r recall summary  score"

*wave 2 spouse numeracy  score
gen s2tr20 =.
spouse r2tr20, result(s2tr20) wave(2)
label variable s2tr20 "s2tr20:w2 s recall summary  score"

***Whether Factors Impaired Cognition Tests***
*wave 2 respondent whether factors impaired cognition tests
gen r2cogimp = .
missing_common cf017, result(r2cogimp)
replace r2cogimp = 0 if cf017 == 5
replace r2cogimp = 1 if cf017 == 1
label variable r2cogimp "r2cogimp:w2 r whether factors impaired cognition tests"
label values r2cogimp cogfac

*wave 2 spouse whether factors impaired cognition tests
gen s2cogimp = .
spouse r2cogimp, result(s2cogimp) wave(2)
label variable s2cogimp "s2cogimp:w2 s whether factors impaired cognition tests"
label values s2cogimp cogfac

***Whether Other People Present During Cog Tests***
*wave 2 respondent whether other people present during cog tests
gen r2cogothp = .
missing_common cf018d1 cf018d2 cf018d3 cf018d4, result(r2cogothp)
replace r2cogothp = 0 if cf018d1 == 1 | cf018d2 == 0 | cf018d3 == 0 | cf018d4 == 0
replace r2cogothp = 1 if cf018d1 == 0
label variable r2cogothp "r2cogothp:w2 r whether other people present during cog tests"
label values r2cogothp cogfac

*wave 2 spouse whether other people present during cog tests
gen s2cogothp = .
spouse r2cogothp, result(s2cogothp) wave(2)
label variable s2cogothp "s2cogothp:w2 s whether other people present during cog tests"
label values s2cogothp cogfac


***drop wave 2 Cognitive Function file raw variables
drop `cog_w2_cf'

***drop wave 2 Generated Variables (Health) file raw variables
drop `cog_w2_gv_health'

******************************************************************************************


*set wave number
local wv = 4

***merge with wave 4 cover screen data***
local cog_w4_cv_r firstwave   
merge 1:1 mergeid using "$wave_4_cv_r", keepusing(`cog_w4_cv_r') nogen

***merge with wave 4 Cognitive Function data***
local cog_w4_cf cf003_ cf004_ cf005_ cf006_ cf010_ cf103_ cf108_ cf109_ cf110_ cf111_ cf112_ cf017_ cf018d*
merge 1:1 mergeid using "$wave_4_cf", keepusing(`cog_w4_cf') nogen

***merge with wave 4 Generated Variables (Health) data***
local cog_w4_gv_health numeracy orienti cf008tot cf016tot numeracy2
merge 1:1 mergeid using "$wave_4_gv_health", keepusing(`cog_w4_gv_health') nogen


***immediate word recall***
*wave 4 respondent immediate word recall
gen r4imrc =.
missing_common cf008tot, result(r4imrc)
replace r4imrc = cf008tot if inrange(cf008tot,0,10)
replace r4imrc = .p if r4imrc==.m & inlist(r4proxy,1,2)
label variable r4imrc "r4imrc:w4 r immediate word recall"

*wave 4 spouse immediate word recall
gen s4imrc =.
spouse r4imrc, result(s4imrc) wave(4)
label variable s4imrc "s4imrc:w4 s immediate word recall"

***delayed word recall***
*wave 4 respondent delayed word recall
gen r4dlrc =.
missing_common cf016tot, result(r4dlrc)
replace r4dlrc = cf016tot if inrange(cf016tot,0,10)
replace r4dlrc = .p if r4dlrc==.m & inlist(r4proxy,1,2)
label variable r4dlrc "r4dlrc:w4 r delayed word recall"

*wave 4 spouse delayed word recall
gen s4dlrc =.
spouse r4dlrc, result(s4dlrc) wave(4)
label variable s4dlrc "s4dlrc:w4 s delayed word recall"

***cognition month naming***
*wave 4 respondent cognition month naming
gen r4mo= .
missing_common cf004_, result(r4mo)
replace r4mo = .n if firstwave<4 & inw`wv' == 1
replace r4mo = 1 if cf004_==1 
replace r4mo = 0 if cf004_==2
label variable r4mo "r4mo:w4 r cognition date naming-month"
label values r4mo recall

*wave 4 spouse month naming
gen s4mo =.
spouse r4mo, result(s4mo) wave(4)
label variable s4mo "s4mo:w4 s cognition date naming-month"
label values s4mo recall

***cognition day naming***
*wave 4 respondent cognition day naming
gen r4dy= .
missing_common cf003_, result(r4dy)
replace r4dy = .n if firstwave<4 & inw`wv' == 1
replace r4dy = 1 if cf003_==1 
replace r4dy = 0 if cf003_==2
label variable r4dy "r4dy:w4 r cognition date naming-day of month"
label values r4dy recall

*wave 4 spouse day naming
gen s4dy =.
spouse r4dy, result(s4dy) wave(4)
label variable s4dy "s4dy:w4 s cognition date naming-day of month"
label values s4dy recall

***cognition year naming***
*wave 4 respondent cognition year naming
gen r4yr= .
missing_common cf005_, result(r4yr)
replace r4yr = .n if firstwave<4 & inw`wv' == 1
replace r4yr = 1 if cf005_==1 
replace r4yr = 0 if cf005_==2
label variable r4yr "r4yr:w4 r cognition date naming-year"
label values r4yr recall

*wave 4 spouse year naming
gen s4yr =.
spouse r4yr, result(s4yr) wave(4)
label variable s4yr "s4yr:w4 s cognition date naming-year"
label values s4yr recall

***cognition day of week naming***
*wave 4 respondent cognition day of week naming
gen r4dw= .
missing_common cf006_, result(r4dw)
replace r4dw = .n if firstwave<4 & inw`wv' == 1
replace r4dw = 1 if cf006_==1 
replace r4dw = 0 if cf006_==2
label variable r4dw "r4dw:w4 r cognition date naming-day of week"
label values r4dw recall

*wave 4 spouse day of week naming
gen s4dw =.
spouse r4dw, result(s4dw) wave(4)
label variable s4dw "s4dw:w4 s cognition date naming-day of week"
label values s4dw recall

***cognition orient***
*wave 4 respondent cognition orient
gen r4orient=.
replace r4orient=.n if firstwave<4 & inw`wv' == 1
replace r4orient=orienti if !mi(orienti)
replace r4orient = .m if (r4mo==.m | r4dy==.m | r4yr==.m | r4dw==.m) & mi(r4orient)
replace r4orient = .d if (r4mo==.d | r4dy==.d | r4yr==.d | r4dw==.d) & inlist(r4orient,.,.m)
replace r4orient = .r if (r4mo==.r | r4dy==.r | r4yr==.r | r4dw==.r) & inlist(r4orient,.,.m)
label variable r4orient "r4orient:w4 r cognition orient (summary date naming)"

*wave 4 spouse cognition orient
gen s4orient =.
spouse r4orient, result(s4orient) wave(4)
label variable s4orient "s4orient:w4 s cognition orient (summary date naming)"

***verbal fluency score***
*wave 4 respondent verbal fluency score
gen r4verbf= .
missing_common cf010_, result(r4verbf)
replace r4verb = cf010_ if inrange(cf010_,0,100)
replace r4verb = .p if r4verb==.m & inlist(r4proxy,1,2)
label variable r4verbf "r4verbf:w4 r verbal fluency score"

*wave 4 spouse verbal fluency score
gen s4verbf =.
spouse r4verbf, result(s4verbf) wave(4)
label variable s4verbf "s4verbf:w4 s verbal fluency score"

***numeracy  score***
*wave 4 respondent numeracy  score
gen r4numer_s= .
missing_common numeracy, result(r4numer_s)
replace r4numer_s=.n if firstwave<4 & inw`wv' == 1
replace r4numer_s = numeracy if inrange(numeracy,1,5)
label variable r4numer_s "r4numer_s:w4 r numeracy  score"

*wave 4 spouse numeracy  score
gen s4numer_s =.
spouse r4numer_s, result(s4numer_s) wave(4)
label variable s4numer_s "s4numer_s:w4 s numeracy  score"

***recall summary  score***
*wave 4 respondent recall summary  score
gen r4tr20 = r4imrc + r4dlrc 
missing_h r4imrc r4dlrc if r4tr20==., result(r4tr20)
replace r4tr20 = .p if r4imrc==.p & r4dlrc==.p
label variable r4tr20 "r4tr20:w4 r recall summary  score"

*wave 4 spouse numeracy  score
gen s4tr20 =.
spouse r4tr20, result(s4tr20) wave(4)
label variable s4tr20 "s4tr20:w4 s recall summary  score"

***only in wave4***
***self-rated memory***
*wave 4 respondent self-rated memory
gen r4slfmem= .
missing_common cf103_, result(r4slfmem)
replace r4slfmem = cf103_  if inrange(cf103_,1,5)
label variable r4slfmem "r4slfmem:w4 r self-rated memory"
label values r4slfmem health

*wave 4 spouse self-rated memory
gen s4slfmem =.
spouse r4slfmem, result(s4slfmem) wave(4)
label variable s4slfmem "s4slfmem:w4 s self-rated memory"
label values s4slfmem  health

***serial 7s***
*wave 4 respondent serial 7s
gen r4ser7=.
replace r4ser7= .m if (cf108_==. | cf109_==. | cf110_==. | cf111_==. | cf112_==.) & inw`wv' == 1
replace r4ser7= .d if cf108_==-1 | cf109_==-1 | cf110_==-1 | cf111_==-1 | cf112_==-1
replace r4ser7= .r if cf108_==-2 | cf109_==-2 | cf110_==-2 | cf111_==-2 | cf112_==-2
replace r4ser7= 0 if inrange(cf108_,0,99999999)
replace r4ser7= r4ser7+1 if cf108_==93
replace r4ser7= r4ser7+1 if cf109_==(cf108_ - 7) & !mi(cf109_) & !mi(cf108_)
replace r4ser7= r4ser7+1 if cf110_==(cf109_ - 7) & !mi(cf110_) & !mi(cf109_)
replace r4ser7= r4ser7+1 if cf111_==(cf110_ - 7) & !mi(cf111_) & !mi(cf110_)
replace r4ser7= r4ser7+1 if cf112_==(cf111_ - 7) & !mi(cf112_) & !mi(cf111_)
replace r4ser7= .p if r4ser7==.m & inlist(r4proxy,1,2)
label variable r4ser7  "r4ser7:w4 r serial 7s"

*wave 4 spouse sserial 7s
gen s4ser7 =.
spouse r4ser7, result(s4ser7) wave(4)
label variable s4ser7 "s4ser7:w4 s serial 7s"

***Whether Factors Impaired Cognition Tests***
*wave 4 respondent whether factors impaired cognition tests
gen r4cogimp = .
missing_common cf017, result(r4cogimp)
replace r4cogimp = 0 if cf017 == 5
replace r4cogimp = 1 if cf017 == 1
label variable r4cogimp "r4cogimp:w4 r whether factors impaired cognition tests"
label values r4cogimp cogfac

*wave 4 spouse whether factors impaired cognition tests
gen s4cogimp = .
spouse r4cogimp, result(s4cogimp) wave(4)
label variable s4cogimp "s4cogimp:w4 s whether factors impaired cognition tests"
label values s4cogimp cogfac

***Whether Other People Present During Cog Tests***
*wave 4 respondent whether other people present during cog tests
gen r4cogothp = .
missing_common cf018d1 cf018d2 cf018d3 cf018d4, result(r4cogothp)
replace r4cogothp = 0 if cf018d1 == 1 | cf018d2 == 0 | cf018d3 == 0 | cf018d4 == 0
replace r4cogothp = 1 if cf018d1 == 0
label variable r4cogothp "r4cogothp:w4 r whether other people present during cog tests"
label values r4cogothp cogfac

*wave 4 spouse whether other people present during cog tests
gen s4cogothp = .
spouse r4cogothp, result(s4cogothp) wave(4)
label variable s4cogothp "s4cogothp:w4 s whether other people present during cog tests"
label values s4cogothp cogfac



***drop wave 4 cover screen file raw variables
drop `cog_w4_cv_r'

***drop wave 4 Cognitive Function file raw variables
drop `cog_w4_cf'

***drop wave 4 Generated Variables (Health) file raw variables
drop `cog_w4_gv_health'

******************************************************************************************


*set wave number
local wv = 5

***merge with wave 5 cover screen data***
local cog_w5_cv_r firstwave   
merge 1:1 mergeid using "$wave_5_cv_r", keepusing(`cog_w5_cv_r') nogen

***merge with wave 5 Cognitive Function data***
local cog_w5_cf cf003_ cf004_ cf005_ cf006_ cf010_ cf103_ cf108_ cf109_ cf110_ cf111_ cf112_ cf017_ cf018d*
merge 1:1 mergeid using "$wave_5_cf", keepusing(`cog_w5_cf') nogen

***merge with wave 5 Generated Variables (Health) data***
local cog_w5_gv_health numeracy orienti cf008tot cf016tot
merge 1:1 mergeid using "$wave_5_gv_health", keepusing(`cog_w5_gv_health') nogen


***immediate word recall***
*wave 5 respondent immediate word recall
gen r5imrc =.
missing_common cf008tot, result(r5imrc)
replace r5imrc = cf008tot if inrange(cf008tot,0,10)
replace r5imrc = .p if r5imrc==.m & inlist(r5proxy,1,2)
label variable r5imrc "r5imrc:w5 r immediate word recall"

*wave 5 spouse immediate word recall
gen s5imrc =.
spouse r5imrc, result(s5imrc) wave(5)
label variable s5imrc "s5imrc:w5 s immediate word recall"

***delayed word recall***
*wave 5 respondent delayed word recall
gen r5dlrc =.
missing_common cf016tot, result(r5dlrc)
replace r5dlrc = cf016tot if inrange(cf016tot,0,10)
replace r5dlrc = .p if r5dlrc==.m & inlist(r5proxy,1,2)
label variable r5dlrc "r5dlrc:w5 r delayed word recall"

*wave 5 spouse delayed word recall
gen s5dlrc =.
spouse r5dlrc, result(s5dlrc) wave(5)
label variable s5dlrc "s5dlrc:w5 s delayed word recall"

***cognition month naming***
*wave 5 respondent cognition month naming
gen r5mo= .
missing_common cf004_, result(r5mo)
replace r5mo = .n if firstwave<5 & inw`wv' == 1
replace r5mo = 1 if cf004_==1 
replace r5mo = 0 if cf004_==2
label variable r5mo "r5mo:w5 r cognition date naming-month"
label values r5mo recall

*wave 5 spouse month naming
gen s5mo =.
spouse r5mo, result(s5mo) wave(5)
label variable s5mo "s5mo:w5 s cognition date naming-month"
label values s5mo recall

***cognition day naming***
*wave 5 respondent cognition day naming
gen r5dy= .
missing_common cf003_, result(r5dy)
replace r5dy = .n if firstwave<5 & inw`wv' == 1
replace r5dy = 1 if cf003_==1 
replace r5dy = 0 if cf003_==2
label variable r5dy "r5dy:w5 r cognition date naming-day of month"
label values r5dy recall

*wave 5 spouse day naming
gen s5dy =.
spouse r5dy, result(s5dy) wave(5)
label variable s5dy "s5dy:w5 s cognition date naming-day of month"
label values s5dy recall

***cognition year naming***
*wave 5 respondent cognition year naming
gen r5yr= .
missing_common cf005_, result(r5yr)
replace r5yr = .n if firstwave<5 & inw`wv' == 1
replace r5yr = 1 if cf005_==1 
replace r5yr = 0 if cf005_==2
label variable r5yr "r5yr:w5 r cognition date naming-year"
label values r5yr recall

*wave 5 spouse year naming
gen s5yr =.
spouse r5yr, result(s5yr) wave(5)
label variable s5yr "s5yr:w5 s cognition date naming-year"
label values s5yr recall

***cognition day of week naming***
*wave 5 respondent cognition day of week naming
gen r5dw= .
missing_common cf006_, result(r5dw)
replace r5dw = .n if firstwave<5 & inw`wv' == 1
replace r5dw = 1 if cf006_==1 
replace r5dw = 0 if cf006_==2
label variable r5dw "r5dw:w5 r cognition date naming-day of week"
label values r5dw recall

*wave 5 spouse day of week naming
gen s5dw =.
spouse r5dw, result(s5dw) wave(5)
label variable s5dw "s5dw:w5 s cognition date naming-day of week"
label values s5dw recall

***cognition orient***
*wave 5 respondent cognition orient
gen r5orient=.
replace r5orient=.n if firstwave<5 & inw`wv' == 1
replace r5orient=orienti if !mi(orienti)
replace r5orient = .m if (r5mo==.m | r5dy==.m | r5yr==.m | r5dw==.m) & mi(r5orient)
replace r5orient = .d if (r5mo==.d | r5dy==.d | r5yr==.d | r5dw==.d) & inlist(r5orient,.,.m)
replace r5orient = .r if (r5mo==.r | r5dy==.r | r5yr==.r | r5dw==.r) & inlist(r5orient,.,.m)
label variable r5orient "r5orient:w5 r cognition orient (summary date naming)"

*wave 5 spouse cognition orient
gen s5orient =.
spouse r5orient, result(s5orient) wave(5)
label variable s5orient "s5orient:w5 s cognition orient (summary date naming)"

***verbal fluency score***
*wave 5 respondent verbal fluency score
gen r5verbf= .
missing_common cf010_, result(r5verbf)
replace r5verb = cf010_ if inrange(cf010_,0,100)
replace r5verb = .p if r5verb==.m & inlist(r5proxy,1,2)
label variable r5verbf "r5verbf:w5 r verbal fluency score"

*wave 5 spouse verbal fluency score
gen s5verbf =.
spouse r5verbf, result(s5verbf) wave(5)
label variable s5verbf "s5verbf:w5 s verbal fluency score"

***numeracy  score***
*wave 5 respondent numeracy  score
gen r5numer_s= .
missing_common numeracy, result(r5numer_s)
replace r5numer_s=.n if firstwave<5 & inw`wv' == 1
replace r5numer_s = numeracy if inrange(numeracy,1,5)
label variable r5numer_s "r5numer_s:w5 r numeracy  score"

*wave 5 spouse numeracy  score
gen s5numer_s =.
spouse r5numer_s, result(s5numer_s) wave(5)
label variable s5numer_s "s5numer_s:w5 s numeracy  score"

***recall summary  score***
*wave 5 respondent recall summary  score
gen r5tr20 = r5imrc + r5dlrc
missing_h r5imrc r5dlrc if r5tr20==., result(r5tr20)
replace r5tr20 = .p if r5imrc==.p & r5dlrc==.p
label variable r5tr20 "r5tr20:w5 r recall summary  score"

*wave 5 spouse numeracy  score
gen s5tr20 =.
spouse r5tr20, result(s5tr20) wave(5)
label variable s5tr20 "s5tr20:w5 s recall summary  score"

***starting in wave4***
***self-rated memory***
*wave 5 respondent self-rated memory
gen r5slfmem= .
missing_common cf103_, result(r5slfmem)
replace r5slfmem = cf103_  if inrange(cf103_,1,5)
label variable r5slfmem "r5slfmem:w5 r self-rated memory"
label values r5slfmem health

*wave 5 spouse self-rated memory
gen s5slfmem =.
spouse r5slfmem, result(s5slfmem) wave(5)
label variable s5slfmem "s5slfmem:w5 s self-rated memory"
label values s5slfmem  health

***serial 7s***
*wave 5 respondent serial 7s
gen r5ser7=.
replace r5ser7= .m if (cf108_==. | cf109_==. | cf110_==. | cf111_==. | cf112_==.) & inw`wv' == 1
replace r5ser7= .d if cf108_==-1 | cf109_==-1 | cf110_==-1 | cf111_==-1 | cf112_==-1
replace r5ser7= .r if cf108_==-2 | cf109_==-2 | cf110_==-2 | cf111_==-2 | cf112_==-2
replace r5ser7= 0 if inrange(cf108_,0,99999999)
replace r5ser7= r5ser7+1 if cf108_==93
replace r5ser7= r5ser7+1 if cf109_==(cf108_ - 7) & !mi(cf109_) & !mi(cf108_)
replace r5ser7= r5ser7+1 if cf110_==(cf109_ - 7) & !mi(cf110_) & !mi(cf109_)
replace r5ser7= r5ser7+1 if cf111_==(cf110_ - 7) & !mi(cf111_) & !mi(cf110_)
replace r5ser7= r5ser7+1 if cf112_==(cf111_ - 7) & !mi(cf112_) & !mi(cf111_)
replace r5ser7= .p if r5ser7==.m & inlist(r5proxy,1,2)
label variable r5ser7  "r5ser7:w5 r serial 7s"

*wave 5 spouse sserial 7s
gen s5ser7 =.
spouse r5ser7, result(s5ser7) wave(5)
label variable s5ser7 "s5ser7:w5 s serial 7s"

***Whether Factors Impaired Cognition Tests***
*wave 5 respondent whether factors impaired cognition tests
gen r5cogimp = .
missing_common cf017, result(r5cogimp)
replace r5cogimp = 0 if cf017 == 5
replace r5cogimp = 1 if cf017 == 1
label variable r5cogimp "r5cogimp:w5 r whether factors impaired cognition tests"
label values r5cogimp cogfac

*wave 5 spouse whether factors impaired cognition tests
gen s5cogimp = .
spouse r5cogimp, result(s5cogimp) wave(5)
label variable s5cogimp "s5cogimp:w5 s whether factors impaired cognition tests"
label values s5cogimp cogfac

***Whether Other People Present During Cog Tests***
*wave 5 respondent whether other people present during cog tests
gen r5cogothp = .
missing_common cf018d1 cf018d2 cf018d3 cf018d4, result(r5cogothp)
replace r5cogothp = 0 if cf018d1 == 1 | cf018d2 == 0 | cf018d3 == 0 | cf018d4 == 0
replace r5cogothp = 1 if cf018d1 == 0
label variable r5cogothp "r5cogothp:w5 r whether other people present during cog tests"
label values r5cogothp cogfac

*wave 5 spouse whether other people present during cog tests
gen s5cogothp = .
spouse r5cogothp, result(s5cogothp) wave(5)
label variable s5cogothp "s5cogothp:w5 s whether other people present during cog tests"
label values s5cogothp cogfac



***drop wave 5 cover screen file raw variables
drop `cog_w5_cv_r'

***drop wave 5 Cognitive Function file raw variables
drop `cog_w5_cf'

***drop wave 5 Generated Variables (Health) file raw variables
drop `cog_w5_gv_health'

******************************************************************************************

*set wave number
local wv = 6

***merge with wave 6 cover screen data***
local cog_w6_cv_r firstwave
merge 1:1 mergeid using "$wave_6_cv_r", keepusing(`cog_w6_cv_r') nogen

***merge with wave 6 Cognitive Function data***
local cog_w6_cf cf003_ cf004_ cf005_ cf006_ cf010_ cf103_ cf108_ cf109_ cf110_ cf111_ cf112_ cf017_ cf018d*
merge 1:1 mergeid using "$wave_6_cf", keepusing(`cog_w6_cf') nogen

***merge with wave 6 Generated Variables (Health) data***
local cog_w6_gv_health numeracy orienti cf008tot cf016tot
merge 1:1 mergeid using "$wave_6_gv_health", keepusing(`cog_w6_gv_health') nogen

***merge with wave 6 Technical Variables***
local cog_w6_techvar mn101_
merge 1:1 mergeid using "$wave_6_techvar", keepusing(`cog_w6_techvar') nogen


***immediate word recall***
*wave 6 respondent immediate word recall
gen r6imrc =.
missing_common cf008tot, result(r6imrc)
replace r6imrc = cf008tot if inrange(cf008tot,0,10)
replace r6imrc = .p if r6imrc==.m & inlist(r6proxy,1,2)
label variable r6imrc "r6imrc:w6 r immediate word recall"

*wave 6 spouse immediate word recall
gen s6imrc =.
spouse r6imrc, result(s6imrc) wave(6)
label variable s6imrc "s6imrc:w6 s immediate word recall"

***delayed word recall***
*wave 6 respondent delayed word recall
gen r6dlrc =.
missing_common cf016tot, result(r6dlrc)
replace r6dlrc = cf016tot if inrange(cf016tot,0,10)
replace r6dlrc = .p if r6dlrc==.m & inlist(r6proxy,1,2)
label variable r6dlrc "r6dlrc:w6 r delayed word recall"

*wave 6 spouse delayed word recall
gen s6dlrc =.
spouse r6dlrc, result(s6dlrc) wave(6)
label variable s6dlrc "s6dlrc:w6 s delayed word recall"

***cognition month naming***
*wave 6 respondent cognition month naming
gen r6mo =.
missing_common cf004_, result(r6mo)
replace r6mo = 1 if cf004_==1
replace r6mo = 0 if cf004_==2
replace r6mo = .p if r6mo==.m & inlist(r6proxy,1,2)
label variable r6mo "r6mo:w6 r cognition date naming-month"
label values r6mo recall

*wave 6 spouse month naming
gen s6mo =.
spouse r6mo, result(s6mo) wave(6)
label variable s6mo "s6mo:w6 s cognition date naming-month"
label values s6mo recall

***cognition day naming***
*wave 6 respondent cognition day naming
gen r6dy =.
missing_common cf003_, result(r6dy)
replace r6dy = 1 if cf003_==1
replace r6dy = 0 if cf003_==2
replace r6dy = .p if r6dy==.m & inlist(r6proxy,1,2)
label variable r6dy "r6dy:w6 r cognition date naming-day of month"
label values r6dy recall

*wave 6 spouse day naming
gen s6dy =.
spouse r6dy, result(s6dy) wave(6)
label variable s6dy "s6dy:w6 s cognition date naming-day of month"
label values s6dy recall

***cognition year naming***
*wave 6 respondent cognition year naming
gen r6yr =.
missing_common cf005_, result(r6yr)
replace r6yr = 1 if cf005_==1
replace r6yr = 0 if cf005_==2
replace r6yr = .p if r6yr==.m & inlist(r6proxy,1,2)
label variable r6yr "r6yr:w6 r cognition date naming-year"
label values r6yr recall

*wave 6 spouse year naming
gen s6yr =.
spouse r6yr, result(s6yr) wave(6)
label variable s6yr "s6yr:w6 s cognition date naming-year"
label values s6yr recall

***cognition day of week naming***
*wave 6 respondent cognition day of week naming
gen r6dw =.
missing_common cf006_, result(r6dw)
replace r6dw = 1 if cf006_==1
replace r6dw = 0 if cf006_==2
replace r6dw = .p if r6dw==.m & inlist(r6proxy,1,2)
label variable r6dw "r6dw:w6 r cognition date naming-day of week"
label values r6dw recall

*wave 6 spouse day of week naming
gen s6dw =.
spouse r6dw, result(s6dw) wave(6)
label variable s6dw "s6dw:w6 s cognition date naming-day of week"
label values s6dw recall

***cognition orient***
*wave 6 respondent cognition orient
gen r6orient =.
replace r6orient=orienti if !mi(orienti)
replace r6orient = .m if (r6mo==.m | r6dy==.m | r6yr==.m | r6dw==.m) & mi(r6orient)
replace r6orient = .d if (r6mo==.d | r6dy==.d | r6yr==.d | r6dw==.d) & inlist(r6orient,.,.m)
replace r6orient = .r if (r6mo==.r | r6dy==.r | r6yr==.r | r6dw==.r) & inlist(r6orient,.,.m)
replace r6orient = .p if (r6mo==.p | r6dy==.p | r6yr==.p | r6dw==.p) & inlist(r6orient,.,.m)
label variable r6orient "r6orient:w6 r cognition orient (summary date naming)"

*wave 6 spouse cognition orient
gen s6orient =.
spouse r6orient, result(s6orient) wave(6)
label variable s6orient "s6orient:w6 s cognition orient (summary date naming)"

***verbal fluency score***
*wave 6 respondent verbal fluency score
gen r6verbf =.
missing_common cf010_, result(r6verbf)
replace r6verb = cf010_ if inrange(cf010_,0,100)
replace r6verb = .p if r6verb==.m & inlist(r6proxy,1,2)
label variable r6verbf "r6verbf:w6 r verbal fluency score"

*wave 6 spouse verbal fluency score
gen s6verbf =.
spouse r6verbf, result(s6verbf) wave(6)
label variable s6verbf "s6verbf:w6 s verbal fluency score"

***numeracy  score***
*wave 6 respondent numeracy  score
gen r6numer_s =.
missing_common numeracy, result(r6numer_s)
replace r6numer_s=.n if firstwave<6 & inw`wv' == 1
replace r6numer_s = numeracy if inrange(numeracy,1,5)
label variable r6numer_s "r6numer_s:w6 r numeracy  score"

*wave 6 spouse numeracy  score
gen s6numer_s =.
spouse r6numer_s, result(s6numer_s) wave(6)
label variable s6numer_s "s6numer_s:w6 s numeracy  score"

***recall summary  score***
*wave 6 respondent recall summary  score
gen r6tr20 = r6imrc + r6dlrc
missing_h r6imrc r6dlrc if r6tr20==., result(r6tr20)
replace r6tr20 = .p if r6imrc==.p & r6dlrc==.p
label variable r6tr20 "r6tr20:w6 r recall summary  score"

*wave 6 spouse numeracy  score
gen s6tr20 =.
spouse r6tr20, result(s6tr20) wave(6)
label variable s6tr20 "s6tr20:w6 s recall summary  score"

***starting in wave4***
***self-rated memory***
*wave 6 respondent self-rated memory
gen r6slfmem =.
missing_common cf103_, result(r6slfmem)
replace r6slfmem = .n if mn101_==1
replace r6slfmem = cf103_  if inrange(cf103_,1,5)
label variable r6slfmem "r6slfmem:w6 r self-rated memory"
label values r6slfmem health

*wave 6 spouse self-rated memory
gen s6slfmem =.
spouse r6slfmem, result(s6slfmem) wave(6)
label variable s6slfmem "s6slfmem:w6 s self-rated memory"
label values s6slfmem  health

***serial 7s***
*wave 6 respondent serial 7s
gen r6ser7 =.
replace r6ser7= .m if (cf108_==. | cf109_==. | cf110_==. | cf111_==. | cf112_==.) & inw`wv' == 1
replace r6ser7= .d if cf108_==-1 | cf109_==-1 | cf110_==-1 | cf111_==-1 | cf112_==-1
replace r6ser7= .r if cf108_==-2 | cf109_==-2 | cf110_==-2 | cf111_==-2 | cf112_==-2
replace r6ser7= 0 if inrange(cf108_,0,99999999)
replace r6ser7= r6ser7+1 if cf108_==93
replace r6ser7= r6ser7+1 if cf109_==(cf108_ - 7) & !mi(cf109_) & !mi(cf108_)
replace r6ser7= r6ser7+1 if cf110_==(cf109_ - 7) & !mi(cf110_) & !mi(cf109_)
replace r6ser7= r6ser7+1 if cf111_==(cf110_ - 7) & !mi(cf111_) & !mi(cf110_)
replace r6ser7= r6ser7+1 if cf112_==(cf111_ - 7) & !mi(cf112_) & !mi(cf111_)
replace r6ser7= .p if r6ser7==.m & inlist(r6proxy,1,2)
label variable r6ser7  "r6ser7:w6 r serial 7s"

*wave 6 spouse sserial 7s
gen s6ser7 =.
spouse r6ser7, result(s6ser7) wave(6)
label variable s6ser7 "s6ser7:w6 s serial 7s"

***Whether Factors Impaired Cognition Tests***
*wave 6 respondent whether factors impaired cognition tests
gen r6cogimp =.
missing_common cf017, result(r6cogimp)
replace r6cogimp = 0 if cf017 == 5
replace r6cogimp = 1 if cf017 == 1
replace r6cogimp = .p if r6cogimp==.m & inlist(r6proxy,1,2)
label variable r6cogimp "r6cogimp:w6 r whether factors impaired cognition tests"
label values r6cogimp cogfac

*wave 6 spouse whether factors impaired cognition tests
gen s6cogimp =.
spouse r6cogimp, result(s6cogimp) wave(6)
label variable s6cogimp "s6cogimp:w6 s whether factors impaired cognition tests"
label values s6cogimp cogfac

***Whether Other People Present During Cog Tests***
*wave 6 respondent whether other people present during cog tests
gen r6cogothp =.
missing_common cf018d1 cf018d2 cf018d3 cf018d4, result(r6cogothp)
replace r6cogothp = 0 if cf018d1 == 1 | cf018d2 == 0 | cf018d3 == 0 | cf018d4 == 0
replace r6cogothp = 1 if cf018d1 == 0
replace r6cogothp = .p if r6cogothp==.m & inlist(r6proxy,1,2)
label variable r6cogothp "r6cogothp:w6 r whether other people present during cog tests"
label values r6cogothp cogfac

*wave 6 spouse whether other people present during cog tests
gen s6cogothp =.
spouse r6cogothp, result(s6cogothp) wave(6)
label variable s6cogothp "s6cogothp:w6 s whether other people present during cog tests"
label values s6cogothp cogfac



***drop wave 6 cover screen file raw variables
drop `cog_w6_cv_r'

***drop wave 6 Cognitive Function file raw variables
drop `cog_w6_cf'

***drop wave 6 Generated Variables (Health) file raw variables
drop `cog_w6_gv_health'

***drop wave 6 technical variables file raw variables***
drop `cog_w6_techvar'

******************************************************************************************

***Currency
label define currencies ///
	1 "1.Euro" ///
	2 "2.Danish Krone" ///
	3 "3.Swedish Krona" ///
	4 "4.Swiss Franc" ///
	5 "5.Czech Koruna" ///
	6 "6.Polish Zloty"

***Asset imputation flag
label define assimpflag     ///
   -9 "-9.Ireland" ///
   -3 "-3.institutional interview" ///
   -2 "-2.non-responding spouse" ///
   -1 "-1.multiple econ units in hh" ///
    0 "0.No imputation" ///
    1 "1.Imputation" 

***Asset flag
label define assflag ///
  -9 "-9.Ireland" ///
  -3 "-3.institutional interview" ///
  -2 "-2.non-responding spouse" ///
  -1 "-1.multiple econ units in hh" ///
   1 "1.continuous value" ///
   2 "2.complete bracket"       ///
   3 "3.incomplete bracket"     ///
   5 "5.no value/bracket"        ///
   6 "6.no asset"           ///
   7 "7.dk ownership"     ///
   9 "9.non-responding spouse" 
   
***Home ownership
label define homeowner ///
	0 "0.don't own home" ///
	1 "1.own home"

***Home ownership flag
label define ownflag ///
	0 "0.No imputation" ///
	1 "1.Imputation"
    
******************************************************************************************

	

*set wave number
local wv = 1

***merge with wave 1 household data***
local asset_w1_ho ho002_ 
merge 1:1 mergeid using "$wave_1_ho", keepusing(`asset_w1_ho') nogen

***merge with wave 1 imputation data***
local asset_w1_imput vbus   sbus   bacc   bsmf   car   slti   liab    ///
                     vbus_f sbus_f bacc_f bsmf_f car_f slti_f liab_f  ///
                     ypen1   ypen2   ydip   yind ///
                     ypen1_f ypen2_f ydip_f yind_f ///
                     home   mort  ores ores_f  ///
                     home_f mort_f otrf otrf_f ///
                     p_nrp
merge 1:1 mergeid using "$wave_1_imputations_", keepusing(`asset_w1_imput') nogen
 

***create indicator of multiple couples in household
egen cplxhh = tag(hhid1 h1coupid)
egen multihouse = total(cplxhh) if inw1==1, by(hhid1)
recode multihouse (1=0) (2/10 = 1)

//--------------------------------------------------------------------
// Assets
//--------------------------------------------------------------------
// AS section questions are asked to the financial respondent on behalf of 
// the respondent and the spouse, if any spouse. The respondent and spouse
// can also choose to answer questions about their finances separetly in which
// case both the respondent and the spouse answer asset questions. Asset values
// are imputed at the individual level and at the household level.
// Individual level imputations are available for all financial respondents. 
// All non financial respodents are assigned 0 values for the
// indvidual level asset imputation variables. Household-level variables 
// are derived for all respondents.

***create value of share of own business***
generate double ownvbus = vbus*(sbus/100)
label variable ownvbus "value of own share of businesses"

***combine imputation flags for individual business ownership***
generate byte ownvbus_f = .
replace ownvbus_f = 2 if vbus_f == 2 | sbus_f == 2
replace ownvbus_f = 3 if vbus_f == 3 | sbus_f == 3
replace ownvbus_f = 4 if vbus_f == 4 | sbus_f == 4
replace ownvbus_f = 5 if vbus_f == 5 | sbus_f == 5
replace ownvbus_f = 6 if vbus_f == 6 | sbus_f == 6
replace ownvbus_f = 7 if vbus_f == 7 | sbus_f == 7
replace ownvbus_f = 9 if vbus_f == 9 | sbus_f == 9
replace ownvbus_f = 10 if vbus_f == 10 | sbus_f == 10
replace ownvbus_f = 11 if vbus_f == 11 | sbus_f == 11
replace ownvbus_f = 8 if vbus_f == 8 | sbus_f == 8
replace ownvbus_f = 1 if vbus_f == 1 | sbus_f == 1
label variable ownvbus_f "value of own share of businesses - imputation flag"
label values ownvbus_f IMPflag

// asset categories
local ascateg ownvbus bacc bsmf car slti liab 

***create couple-level asset variables
foreach cat in `ascateg' {
    generate double h`wv'a`cat' = `cat'
    label variable h`wv'a`cat' "h`wv'a`cat':w`wv' asset value `cat' (cpl level)"
    
    imputation_flag `cat'_f, gen(h`wv'af`cat') by(h`wv'coupid)
    label variable h`wv'af`cat' "h`wv'af`cat':w`wv' asset impu flag `cat' (cpl level)"
    label values h`wv'af`cat' assflag
}

***create household-level household variables
foreach cat in `ascateg' {
    egen double hh`wv'a`cat' = total(`cat' * (cplxhh==1)) if !mi(hh1hhid), by(hh1hhid)
    label variable hh`wv'a`cat' "hh`wv'a`cat':w`wv' asset value `cat' (hh level)"
    
    imputation_flag `cat'_f, gen(hh`wv'af`cat') by(hh1hhid)
    label variable hh`wv'af`cat' "hh`wv'af`cat':w`wv' asset impu flag `cat' (hh level)"
    label values hh`wv'af`cat' assflag
}

drop ownvbus ownvbus_f

***value of cars
*wave 1 couple-level value of cars
generate double h`wv'atran  = h`wv'acar
label variable h`wv'atran "h`wv'atran:w`wv' Assets:Cars"

*wave 1 couple-level flag for value of cars
generate byte   h`wv'aftran = h`wv'afcar
label variable h`wv'aftran "h`wv'aftran:w`wv' Asst Flag:Cars"
label values h`wv'aftran assflag

drop h`wv'acar h`wv'afcar

*wave 1 household-level value of cars
generate double hh`wv'atran  = hh`wv'acar
label variable hh`wv'atran "hh`wv'atran:w`wv' Assets:Cars"

*wave 1 household-level flag for value of cars
generate byte   hh`wv'aftran = hh`wv'afcar
label variable hh`wv'aftran "hh`wv'aftran:w`wv' Asst Flag:Cars"
label values hh`wv'aftran assflag

drop hh`wv'acar hh`wv'afcar

***value of business
*wave 1 couple-level value of business
generate double h`wv'absns  = h`wv'aownvbus
label variable h`wv'absns "h`wv'absns:w`wv' Assets:Business"

*wave 1 couple-level flag for value of business
generate byte   h`wv'afbsns = h`wv'afownvbus
label variable h`wv'afbsns "h`wv'afbsns:w`wv' Asst Flag:Business"
label values h`wv'afbsns assflag

drop h`wv'aownvbus h`wv'afownvbus

*wave 1 household-level value of business
generate double hh`wv'absns  = hh`wv'aownvbus
label variable hh`wv'absns "hh`wv'absns:w`wv' Assets:Business"

*wave 1 household-level flag for value of business
generate byte   hh`wv'afbsns = hh`wv'afownvbus
label variable hh`wv'afbsns "hh`wv'afbsns:w`wv' Asst Flag:Business"
label values hh`wv'afbsns assflag

drop hh`wv'aownvbus hh`wv'afownvbus

***value of IRAs and other long-term savings
*wave 1 couple-level value of IRAs and other long-term savings
generate double h`wv'airothr  = h`wv'aslti
label variable h`wv'airothr "h`wv'airothr:w`wv' Assets: IRAs and other long-term savings"

*wave 1 couple-level flag for value of IRAs and other long-term savings
generate byte   h`wv'afirothr = h`wv'afslti
label variable h`wv'afirothr "h`wv'afirothr:w`wv' Asst Flag:IRAs and other long-term savings"
label values h`wv'afirothr assflag

drop h`wv'aslti h`wv'afslti

*wave 1 household-level value of IRAs and other long-term savings
generate double hh`wv'airothr  = hh`wv'aslti
label variable hh`wv'airothr "hh`wv'airothr:w`wv' Assets: IRAs and other long-term savings"

*wave 1 household-level flag for value of IRAs and other long-term savings
generate byte   hh`wv'afirothr = hh`wv'afslti
label variable hh`wv'afirothr "hh`wv'afirothr:w`wv' Asst Flag:IRAs and other long-term savings"
label values hh`wv'afirothr assflag

drop hh`wv'aslti hh`wv'afslti

***value of bonds and stocks
*wave 1 couple-level value of bonds and stocks
generate double h`wv'abdstk  = h`wv'absmf
label variable h`wv'abdstk "h`wv'abdstk:w`wv' Assets:Bonds and Stocks"

*wave 1 couple-level flag for value of bonds and stocks
generate byte   h`wv'afbdstk = h`wv'afbsmf
label variable h`wv'afbdstk "h`wv'afbdstk:w`wv' Asst Flag:Bonds and Stocks"
label values h`wv'afbdstk assflag

drop h`wv'absmf h`wv'afbsmf

*wave 1 household-level value of bonds and stocks
generate double hh`wv'abdstk  = hh`wv'absmf
label variable hh`wv'abdstk "hh`wv'abdstk:w`wv' Assets:Bonds and Stocks"

*wave 1 household-level flag for value of bonds and stocks
generate byte   hh`wv'afbdstk = hh`wv'afbsmf
label variable hh`wv'afbdstk "hh`wv'afbdstk:w`wv' Asst Flag:Bonds and Stocks"
label values hh`wv'afbdstk assflag

drop hh`wv'absmf hh`wv'afbsmf

***value of checking, savings acct
*wave 1 couple-level value of checking, savings acct
generate double h`wv'achck  = h`wv'abacc
label variable h`wv'achck "h`wv'achck:w`wv' Assets:Checking,Savings Acct"

*wave 1 couple-level flag for value of checking, savings acct
generate byte   h`wv'afchck = h`wv'afbacc
label variable h`wv'afchck "h`wv'afchck:w`wv' Asst Flag:Checking,Savings Acct"
label values h`wv'afchck assflag

drop h`wv'abacc h`wv'afbacc

*wave 1 household-level value of checking, savings acct
generate double hh`wv'achck  = hh`wv'abacc
label variable hh`wv'achck "hh`wv'achck:w`wv' Assets:Checking,Savings Acct"

*wave 1 household-level flag for value of checking, savings acct
generate byte   hh`wv'afchck = hh`wv'afbacc
label variable hh`wv'afchck "hh`wv'afchck:w`wv' Asst Flag:Checking,Savings Acct"
label values hh`wv'afchck assflag

drop hh`wv'abacc hh`wv'afbacc

***value of debts
*wave 1 couple-level value of debts
generate double h`wv'adebt  = h`wv'aliab
label variable h`wv'adebt "h`wv'adebt:w`wv' Assets:Debts"

*wave 1 couple-level flag for value of debts
generate byte   h`wv'afdebt = h`wv'afliab
label variable h`wv'afdebt "h`wv'afdebt:w`wv' Asst Flag:Debts"
label values h`wv'afdebt assflag

drop h`wv'aliab h`wv'afliab

*wave 1 household-level value of debts
generate double hh`wv'adebt  = hh`wv'aliab
label variable hh`wv'adebt "hh`wv'adebt:w`wv' Assets:Debts"

*wave 1 household-level flag for value of debts
generate byte   hh`wv'afdebt = hh`wv'afliab
label variable hh`wv'afdebt "hh`wv'afdebt:w`wv' Asst Flag:Debts"
label values hh`wv'afdebt assflag

drop hh`wv'aliab hh`wv'afliab

// HO section questions are asked to all household respondents on behalf of 
// the whole household. Even though multiple respondents can complete
// the household section the imputations for these questions are only based
// one of those respondents answers and all members of the household
// are given the same value. Given this couple-level
// variables can only be derived for couples without any additional household
// members. Household-level variables are derived for all respondents.

*generate couple-level value of other real estate variables
generate double h`wv'aores = ores if multihouse == 0
label variable h`wv'aores "h`wv'aores:w`wv' asset value ores (cpl level)"

imputation_flag ores_f if multihouse == 0, gen(h`wv'afores) by(hh`wv'hhid)
replace h`wv'afores = -1 if multihouse == 1
label variable h`wv'afores "h`wv'afores:w`wv' asset impu flag ores (cpl level)"
label values h`wv'afores assflag

*generate household-level value of other real estate variables
generate double hh`wv'aores = ores
label variable hh`wv'aores "hh`wv'aores:w`wv' asset value ores (hh level)"

imputation_flag ores_f, gen(hh`wv'afores) by(hh`wv'hhid)
label variable hh`wv'afores "hh`wv'afores:w`wv' asset impu flag ores (hh level)"
label values hh`wv'afores assflag

*create more complete imputation variable for value of primary residence
gen home_hf = home_f
replace home_hf = 2 if inlist(otrf,3,4,5,6) & otrf_f == 3
replace home_hf = 8 if otrf_f == 14

*generate couple-level value of primary residence variables
generate double h`wv'ahome = home if multihouse == 0
label variable h`wv'ahome "h`wv'ahome:w`wv' asset value home (cpl level)"

imputation_flag home_hf if multihouse == 0, gen(h`wv'afhome) by(hh`wv'hhid)
replace h`wv'afhome = -1 if multihouse == 1
label variable h`wv'afhome "h`wv'afhome:w`wv' asset impu flag home (cpl level)"
label values h`wv'afhome assflag

*generate household-level value of primary residence variables
generate double hh`wv'ahome = home
label variable hh`wv'ahome "hh`wv'ahome:w`wv' asset value home (hh level)"

imputation_flag home_hf, gen(hh`wv'afhome) by(hh`wv'hhid)
label variable hh`wv'afhome "hh`wv'afhome:w`wv' asset impu flag home (hh level)"
label values hh`wv'afhome assflag
drop home_hf

*create more complete imputation variable for value of mortgage
gen mort_hf = mort_f
replace mort_hf = 2 if inlist(otrf,3,4,5,6) & otrf_f == 3

*generate couple-level value of mortgage variables
generate double h`wv'amort = mort if multihouse == 0
label variable h`wv'amort "h`wv'amort:w`wv' asset value mort (cpl level)"

imputation_flag mort_hf if multihouse == 0, gen(h`wv'afmort) by(hh`wv'hhid)
replace h`wv'afmort = -1 if multihouse == 1
label variable h`wv'afmort "h`wv'afmort:w`wv' asset impu flag mort (cpl level)"
label values h`wv'afmort assflag

*generate household-level value of mortage variables
generate double hh`wv'amort = mort
label variable hh`wv'amort "hh`wv'amort:w`wv' asset value mort (hh level)"

imputation_flag mort_hf, gen(hh`wv'afmort) by(hh`wv'hhid)
label variable hh`wv'afmort "hh`wv'afmort:w`wv' asset impu flag mort (hh level)"
label values hh`wv'afmort assflag
drop mort_hf

***home ownership***
*wave 1 couple-level home ownership
gen h`wv'ahown = .
replace h`wv'ahown = .m if ho002_ == . & inw`wv' == 1
replace h`wv'ahown = .d if ho002_ == -1
replace h`wv'ahown = .r if ho002_ == -2
replace h`wv'ahown = 0 if inlist(ho002_,2,3,4,5,6)
replace h`wv'ahown = 1 if ho002_ == 1
bysort h`wv'coupid: replace h`wv'ahown = h`wv'ahown[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'ahown = h`wv'ahown[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse
replace h`wv'ahown = .m if h`wv'ahown==. & inw`wv'==1
label var h`wv'ahown "h`wv'ahown:w`wv' whether own home"
label values h`wv'ahown homeowner

***value of other real estate
*wave 1 couple-level value of other real estate
generate double h`wv'arles  = h`wv'aores
label variable h`wv'arles "h`wv'arles:w`wv' Assets:Other Real estate"

*wave 1 couple-level flag for value of other real estate
generate byte   h`wv'afrles = h`wv'afores
label variable h`wv'afrles "h`wv'afrles:w`wv' Asst Flag:Other Real estate"
label values h`wv'afrles assflag

drop h`wv'aores h`wv'afores  

*wave 1 household-level value of other real estate
generate double hh`wv'arles  = hh`wv'aores
label variable hh`wv'arles "hh`wv'arles:w`wv' Assets:Other Real estate"

*wave 1 household-level flag for value of other real estate
generate byte   hh`wv'afrles = hh`wv'afores
label variable hh`wv'afrles "hh`wv'afrles:w`wv' Asst Flag:Other Real estate"
label values hh`wv'afrles assflag

drop hh`wv'aores hh`wv'afores  

***value of primary residence
*wave 1 couple-level value of primary residence
generate double h`wv'ahous  = h`wv'ahome
label variable h`wv'ahous "h`wv'ahous:w`wv' Assets:Primary Residence"

*wave 1 couple-level flag for value of primary residence
generate byte   h`wv'afhous = h`wv'afhome
label variable h`wv'afhous "h`wv'afhous:w`wv' Asst Flag:Primary Residence"
label values h`wv'afhous assflag

*wave 1 household-level value of primary residence
generate double hh`wv'ahous  = hh`wv'ahome
label variable hh`wv'ahous "hh`wv'ahous:w`wv' Assets:Primary Residence"

*wave 1 household-level flag for value of primary residence
generate byte   hh`wv'afhous = hh`wv'afhome
label variable hh`wv'afhous "hh`wv'afhous:w`wv' Asst Flag:Primary Residence"
label values hh`wv'afhous assflag

drop h`wv'ahome h`wv'afhome hh`wv'ahome hh`wv'afhome

***value of mortage
*wave 1 couple-level value of mortgage
label variable h`wv'amort "h`wv'amort:w`wv' Assets:Mortgage"

*wave 1 couple-level flag for value of mortgage
label variable h`wv'afmort "h`wv'afmort:w`wv' Asst Flag:Mortgage"

*wave 1 household-level value of mortgage
label variable hh`wv'amort "hh`wv'amort:w`wv' Assets:Mortgage"

*wave 1 household-level flag for value of mortgage
label variable hh`wv'afmort "hh`wv'afmort:w`wv' Asst Flag:Mortgage"

// hmln : not in SHARE

drop cplxhh multihouse

//--------------------------------------------------------------------
// Summary variables
//--------------------------------------------------------------------

generate double h`wv'atoth = h`wv'ahous - h`wv'amort
label variable h`wv'atoth "h`wv'atoth:w`wv' Net Value of House /prim res"

h_imputation_flag h`wv'afhous h`wv'afmort, gen(h`wv'aftoth)
label variable h`wv'aftoth "h`wv'aftoth:w`wv' Asst Flag:Net Value of House /prim res"
label values h`wv'aftoth assflag

generate double hh`wv'atoth = hh`wv'ahous - hh`wv'amort
label variable hh`wv'atoth  "hh`wv'atoth:w`wv' Net Value of House /prim res"

h_imputation_flag hh`wv'afhous hh`wv'afmort, gen(hh`wv'aftoth)
label variable hh`wv'aftoth "hh`wv'aftoth:w`wv' Asst Flag:Net Value of House /prim res"
label values hh`wv'aftoth assflag

// houb : not in SHARE
// mrtb : not in SHARE
// nethb : not in SHARE

// totf : not possible with imputations as ira wealth is tied to other long-term savings

generate double h`wv'atotb = h`wv'ahous + h`wv'arles + h`wv'atran + h`wv'absns + h`wv'airothr ///
    + h`wv'abdstk + h`wv'achck - h`wv'adebt - h`wv'amort
label variable h`wv'atotb "h`wv'atotb:w`wv' total of all assets inc. 2nd hm"

h_imputation_flag h`wv'afhous h`wv'afrles h`wv'aftran h`wv'afbsns h`wv'afirothr h`wv'afbdstk ///
                  h`wv'afchck h`wv'afdebt h`wv'afmort, gen(h`wv'aftotb)
label variable h`wv'aftotb "h`wv'aftotb:w`wv' Asst Flag:total of all assets inc. 2nd hm"
label values h`wv'aftotb assflag

generate double hh`wv'atotb = hh`wv'ahous + hh`wv'arles + hh`wv'atran + hh`wv'absns + hh`wv'airothr ///
    + hh`wv'abdstk + hh`wv'achck - hh`wv'adebt - hh`wv'amort
label variable hh`wv'atotb "hh`wv'atotb:w`wv' total of all assets inc. 2nd hm"

h_imputation_flag hh`wv'afhous hh`wv'afrles hh`wv'aftran hh`wv'afbsns hh`wv'afirothr hh`wv'afbdstk ///
                  hh`wv'afchck hh`wv'afdebt hh`wv'afmort, gen(hh`wv'aftotb)
label variable hh`wv'aftotb "hh`wv'aftotb:w`wv' Asst Flag:total of all assets inc. 2nd hm"
label values hh`wv'aftotb assflag

// totb : not relevant for SHARE
// totw : not possible with imputations as ira wealth is tied to other long-term savings

generate double h`wv'atotn = h`wv'arles + h`wv'atran + h`wv'absns + h`wv'airothr + h`wv'abdstk + ///
                             h`wv'achck - h`wv'adebt
label variable h`wv'atotn "h`wv'atotn:w`wv' Total Non-Housing Assets"

h_imputation_flag h`wv'afrles h`wv'aftran h`wv'afbsns h`wv'afirothr h`wv'afbdstk ///
                  h`wv'afchck h`wv'afdebt, gen(h`wv'aftotn)
label variable h`wv'aftotn "h`wv'aftotn:w`wv' Asst Flag:Total Non-Housing Assets"
label values h`wv'aftotn assflag

generate double hh`wv'atotn = hh`wv'arles + hh`wv'atran + hh`wv'absns + hh`wv'airothr + hh`wv'abdstk + ///
                              hh`wv'achck - hh`wv'adebt
label variable hh`wv'atotn "hh`wv'atotn:w`wv' Total Non-Housing Assets"

h_imputation_flag hh`wv'afrles hh`wv'aftran hh`wv'afbsns hh`wv'afirothr hh`wv'afbdstk ///
                  hh`wv'afchck hh`wv'afdebt, gen(hh`wv'aftotn)
label variable hh`wv'aftotn "hh`wv'aftotn:w`wv' Asst Flag:Total Non-Housing Assets"
label values hh`wv'aftotn assflag


***drop wave 1 imputation file raw variables***
drop `asset_w1_imput'

***drop wave 1 household raw variables***
drop `asset_w1_ho'

******************************************************************************************

*set wave number
local wv = 2
local pre_wv = 1

***merge with wave 2 household data***
local asset_w2_ho ho002_ ho044_
merge 1:1 mergeid using "$wave_2_ho", keepusing(`asset_w2_ho') nogen

***merge with wave 2 imputation data***
local asset_w2_imput vbus   sbus   bacc   bsmf   car   slti   liab    ///
                     vbus_f sbus_f bacc_f bsmf_f car_f slti_f liab_f  ///
                     ypen1   ypen2   ydip   yind ///
                     ypen1_f ypen2_f ydip_f yind_f ///
                     home   mort  ores ores_f  ///
                     home_f mort_f otrf otrf_f ///
                     p_nrp
merge 1:1 mergeid using "$wave_2_imputations_", keepusing(`asset_w2_imput' ) nogen


***create indicator of multiple couples in household
egen cplxhh = tag(hhid2 h2coupid)
egen multihouse = total(cplxhh) if inw2==1, by(hhid2)
recode multihouse (1=0) (2/10 = 1)

//--------------------------------------------------------------------
// Assets
//--------------------------------------------------------------------
// AS section questions are asked to the financial respondent on behalf of 
// the respondent and the spouse, if any spouse. The imputations for these 
// questions are based on each the finical respondent's answers. If the 
// household has multiple un-coupled respondents then the responses/imputations 
// from all household respondents are summed to produce one value for all 
// members of the household in the imputation. Given this couple-level
// variables can only be derived for couples without any additional household
// members. Household-level variables are derived for all respondents.

***create value of share of own business***
generate double ownvbus = vbus*(sbus/100)
label variable ownvbus "value of own share of businesses"

***combine imputation flags for individual business ownership***
generate byte ownvbus_f = .
replace ownvbus_f = 2 if vbus_f == 2 | sbus_f == 2
replace ownvbus_f = 3 if vbus_f == 3 | sbus_f == 3
replace ownvbus_f = 4 if vbus_f == 4 | sbus_f == 4
replace ownvbus_f = 5 if vbus_f == 5 | sbus_f == 5
replace ownvbus_f = 6 if vbus_f == 6 | sbus_f == 6
replace ownvbus_f = 7 if vbus_f == 7 | sbus_f == 7
replace ownvbus_f = 9 if vbus_f == 9 | sbus_f == 9
replace ownvbus_f = 10 if vbus_f == 10 | sbus_f == 10
replace ownvbus_f = 11 if vbus_f == 11 | sbus_f == 11
replace ownvbus_f = 8 if vbus_f == 8 | sbus_f == 8
replace ownvbus_f = 1 if vbus_f == 1 | sbus_f == 1
label variable ownvbus_f "value of own share of businesses - imputation flag"
label values ownvbus_f IMPflag

// asset categories
local ascateg ownvbus bacc bsmf car slti liab 

***create couple-level asset variables
foreach cat in `ascateg' {
    generate double h`wv'a`cat' = `cat'
    label variable h`wv'a`cat' "h`wv'a`cat':w`wv' asset value `cat' (cpl level)"
    
    imputation_flag `cat'_f, gen(h`wv'af`cat') by(h`wv'coupid)
    label variable h`wv'af`cat' "h`wv'af`cat':w`wv' asset impu flag `cat' (cpl level)"
    label values h`wv'af`cat' assflag
}

***create household-level household variables
foreach cat in `ascateg' {
    egen double hh`wv'a`cat' = total(`cat' * (cplxhh==1)) if !mi(hh`wv'hhid), by(hh`wv'hhid)
    label variable hh`wv'a`cat' "hh`wv'a`cat':w`wv' asset value `cat' (hh level)"
    
    imputation_flag `cat'_f, gen(hh`wv'af`cat') by(hh`wv'hhid)
    label variable hh`wv'af`cat' "hh`wv'af`cat':w`wv' asset impu flag `cat' (hh level)"
    label values hh`wv'af`cat' assflag
}

drop ownvbus ownvbus_f

***value of cars
*wave 2 couple-level value of cars
generate double h`wv'atran  = h`wv'acar
label variable h`wv'atran "h`wv'atran:w`wv' Assets:Cars"

*wave 2 couple-level flag for value of cars
generate byte   h`wv'aftran = h`wv'afcar
label variable h`wv'aftran "h`wv'aftran:w`wv' Asst Flag:Cars"
label values h`wv'aftran assflag

drop h`wv'acar h`wv'afcar

*wave 2 household-level value of cars
generate double hh`wv'atran  = hh`wv'acar
label variable hh`wv'atran "hh`wv'atran:w`wv' Assets:Cars"

*wave 2 household-level flag for value of cars
generate byte   hh`wv'aftran = hh`wv'afcar
label variable hh`wv'aftran "hh`wv'aftran:w`wv' Asst Flag:Cars"
label values hh`wv'aftran assflag

drop hh`wv'acar hh`wv'afcar

***value of business
*wave 2 couple-level value of business
generate double h`wv'absns  = h`wv'aownvbus
label variable h`wv'absns "h`wv'absns:w`wv' Assets:Business"

*wave 2 couple-level flag for value of business
generate byte   h`wv'afbsns = h`wv'afownvbus
label variable h`wv'afbsns "h`wv'afbsns:w`wv' Asst Flag:Business"
label values h`wv'afbsns assflag

drop h`wv'aownvbus h`wv'afownvbus

*wave 2 household-level value of business
generate double hh`wv'absns  = hh`wv'aownvbus
label variable hh`wv'absns "hh`wv'absns:w`wv' Assets:Business"

*wave 2 household-level flag for value of business
generate byte   hh`wv'afbsns = hh`wv'afownvbus
label variable hh`wv'afbsns "hh`wv'afbsns:w`wv' Asst Flag:Business"
label values hh`wv'afbsns assflag

drop hh`wv'aownvbus hh`wv'afownvbus

***value of IRAs and other long-term savings
*wave 2 couple-level value of IRAs and other long-term savings
generate double h`wv'airothr  = h`wv'aslti
label variable h`wv'airothr "h`wv'airothr:w`wv' Assets: IRAs and other long-term savings"

*wave 2 couple-level flag for value of IRAs and other long-term savings
generate byte   h`wv'afirothr = h`wv'afslti
label variable h`wv'afirothr "h`wv'afirothr:w`wv' Asst Flag:IRAs and other long-term savings"
label values h`wv'afirothr assflag

drop h`wv'aslti h`wv'afslti

*wave 2 household-level value of IRAs and other long-term savings
generate double hh`wv'airothr  = hh`wv'aslti
label variable hh`wv'airothr "hh`wv'airothr:w`wv' Assets: IRAs and other long-term savings"

*wave 2 household-level flag for value of IRAs and other long-term savings
generate byte   hh`wv'afirothr = hh`wv'afslti
label variable hh`wv'afirothr "hh`wv'afirothr:w`wv' Asst Flag:IRAs and other long-term savings"
label values hh`wv'afirothr assflag

drop hh`wv'aslti hh`wv'afslti

***value of bonds and stocks
*wave 2 couple-level value of bonds and stocks
generate double h`wv'abdstk  = h`wv'absmf
label variable h`wv'abdstk "h`wv'abdstk:w`wv' Assets:Bonds and Stocks"

*wave 2 couple-level flag for value of bonds and stocks
generate byte   h`wv'afbdstk = h`wv'afbsmf
label variable h`wv'afbdstk "h`wv'afbdstk:w`wv' Asst Flag:Bonds and Stocks"
label values h`wv'afbdstk assflag

drop h`wv'absmf h`wv'afbsmf

*wave 2 household-level value of bonds and stocks
generate double hh`wv'abdstk  = hh`wv'absmf
label variable hh`wv'abdstk "hh`wv'abdstk:w`wv' Assets:Bonds and Stocks"

*wave 2 household-level flag for value of bonds and stocks
generate byte   hh`wv'afbdstk = hh`wv'afbsmf
label variable hh`wv'afbdstk "hh`wv'afbdstk:w`wv' Asst Flag:Bonds and Stocks"
label values hh`wv'afbdstk assflag

drop hh`wv'absmf hh`wv'afbsmf

***value of checking, savings acct
*wave 2 couple-level value of checking, savings acct
generate double h`wv'achck  = h`wv'abacc
label variable h`wv'achck "h`wv'achck:w`wv' Assets:Checking,Savings Acct"

*wave 2 couple-level flag for value of checking, savings acct
generate byte   h`wv'afchck = h`wv'afbacc
label variable h`wv'afchck "h`wv'afchck:w`wv' Asst Flag:Checking,Savings Acct"
label values h`wv'afchck assflag

drop h`wv'abacc h`wv'afbacc

*wave 2 household-level value of checking, savings acct
generate double hh`wv'achck  = hh`wv'abacc
label variable hh`wv'achck "hh`wv'achck:w`wv' Assets:Checking,Savings Acct"

*wave 2 household-level flag for value of checking, savings acct
generate byte   hh`wv'afchck = hh`wv'afbacc
label variable hh`wv'afchck "hh`wv'afchck:w`wv' Asst Flag:Checking,Savings Acct"
label values hh`wv'afchck assflag

drop hh`wv'abacc hh`wv'afbacc

***value of debts
*wave 2 couple-level value of debts
generate double h`wv'adebt  = h`wv'aliab
label variable h`wv'adebt "h`wv'adebt:w`wv' Assets:Debts"

*wave 2 couple-level flag for value of debts
generate byte   h`wv'afdebt = h`wv'afliab
label variable h`wv'afdebt "h`wv'afdebt:w`wv' Asst Flag:Debts"
label values h`wv'afdebt assflag

drop h`wv'aliab h`wv'afliab

*wave 2 household-level value of debts
generate double hh`wv'adebt  = hh`wv'aliab
label variable hh`wv'adebt "hh`wv'adebt:w`wv' Assets:Debts"

*wave 2 household-level flag for value of debts
generate byte   hh`wv'afdebt = hh`wv'afliab
label variable hh`wv'afdebt "hh`wv'afdebt:w`wv' Asst Flag:Debts"
label values hh`wv'afdebt assflag

drop hh`wv'aliab hh`wv'afliab

//--------------------------------------------------------------------
// Housing
//--------------------------------------------------------------------
// HO section questions are asked to the household respondent on behalf of 
// the whole household. The imputations for these questions are based
// the household respondent's answers and all members of the household
// are given the same value. Given this couple-level
// values can only be derived for couples without any additional household
// members. Household-level values can be derived for all respondents.


*generate couple-level value of other real estate variables
generate double h`wv'aores = ores if multihouse == 0
label variable h`wv'aores "h`wv'aores:w`wv' asset value ores (cpl level)"

imputation_flag ores_f if multihouse == 0, gen(h`wv'afores) by(hh`wv'hhid)
replace h`wv'afores = -1 if multihouse == 1
label variable h`wv'afores "h`wv'afores:w`wv' asset impu flag ores (cpl level)"
label values h`wv'afores assflag

*generate household-level value of other real estate variables
generate double hh`wv'aores = ores
label variable hh`wv'aores "hh`wv'aores:w`wv' asset value ores (hh level)"

imputation_flag ores_f, gen(hh`wv'afores) by(hh`wv'hhid)
label variable hh`wv'afores "hh`wv'afores:w`wv' asset impu flag ores (hh level)"
label values hh`wv'afores assflag

*create more complete imputation variable for value of primary residence
gen home_hf = home_f
replace home_hf = 2 if inlist(otrf,3,4,5,6) & otrf_f == 3
replace home_hf = 8 if otrf_f == 14

*generate couple-level value of primary residence variables
generate double h`wv'ahome = home if multihouse == 0
label variable h`wv'ahome "h`wv'ahome:w`wv' asset value home (cpl level)"
imputation_flag home_hf if multihouse == 0, gen(h`wv'afhome) by(hh`wv'hhid)
replace h`wv'afhome = -1 if multihouse == 1
label variable h`wv'afhome "h`wv'afhome:w`wv' asset impu flag home (cpl level)"
label values h`wv'afhome assflag

*generate household-level value of primary residence variables
generate double hh`wv'ahome = home
label variable hh`wv'ahome "hh`wv'ahome:w`wv' asset value home (hh level)"

imputation_flag home_hf, gen(hh`wv'afhome) by(hh`wv'hhid)
label variable hh`wv'afhome "hh`wv'afhome:w`wv' asset impu flag home (hh level)"
label values hh`wv'afhome assflag
drop home_hf

*create more complete imputation variable for value of mortgage
gen mort_hf = mort_f
replace mort_hf = 2 if inlist(otrf,3,4,5,6) & otrf_f == 3

*generate couple-level value of mortgage variables
generate double h`wv'amort = mort if multihouse == 0
label variable h`wv'amort "h`wv'amort:w`wv' asset value mort (cpl level)"

imputation_flag mort_hf if multihouse == 0, gen(h`wv'afmort) by(hh`wv'hhid)
replace h`wv'afmort = -1 if multihouse == 1
label variable h`wv'afmort "h`wv'afmort:w`wv' asset impu flag mort (cpl level)"
label values h`wv'afmort assflag

*generate household-level value of mortage variables
generate double hh`wv'amort = mort
label variable hh`wv'amort "hh`wv'amort:w`wv' asset value mort (hh level)"

imputation_flag mort_hf, gen(hh`wv'afmort) by(hh`wv'hhid)
label variable hh`wv'afmort "hh`wv'afmort:w`wv' asset impu flag mort (hh level)"
label values hh`wv'afmort assflag
drop mort_hf

***home ownership***
*wave 2 couple-level home ownership
gen h`wv'ahown = .
replace h`wv'ahown = .m if ho002_ == . & inw`wv' == 1
replace h`wv'ahown = .d if ho002_ == -1
replace h`wv'ahown = .r if ho002_ == -2
replace h`wv'ahown = h`pre_wv'ahown if inrange(h`pre_wv'ahown,0,1) & ho044_ == 5
replace h`wv'ahown = 0 if inlist(ho002_,2,3,4,5,6)
replace h`wv'ahown = 1 if ho002_ == 1
bysort h`wv'coupid: replace h`wv'ahown = h`wv'ahown[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'ahown = h`wv'ahown[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse
replace h`wv'ahown = .m if h`wv'ahown==. & inw`wv'==1
label var h`wv'ahown "h`wv'ahown:w`wv' whether own home"
label values h`wv'ahown homeowner

***value of other real estate
*wave 2 couple-level value of other real estate
generate double h`wv'arles  = h`wv'aores
label variable h`wv'arles "h`wv'arles:w`wv' Assets:Other Real estate"

*wave 2 couple-level flag for value of other real estate
generate byte   h`wv'afrles = h`wv'afores
label variable h`wv'afrles "h`wv'afrles:w`wv' Asst Flag:Other Real estate"
label values h`wv'afrles assflag

drop h`wv'aores h`wv'afores  

*wave 2 household-level value of other real estate
generate double hh`wv'arles  = hh`wv'aores
label variable hh`wv'arles "hh`wv'arles:w`wv' Assets:Other Real estate"

*wave 2 household-level flag for value of other real estate
generate byte   hh`wv'afrles = hh`wv'afores
label variable hh`wv'afrles "hh`wv'afrles:w`wv' Asst Flag:Other Real estate"
label values hh`wv'afrles assflag

drop hh`wv'aores hh`wv'afores  

***value of primary residence
*wave 2 couple-level value of primary residence
generate double h`wv'ahous  = h`wv'ahome
label variable h`wv'ahous "h`wv'ahous:w`wv' Assets:Primary Residence"

*wave 2 couple-level flag for value of primary residence
generate byte   h`wv'afhous = h`wv'afhome
label variable h`wv'afhous "h`wv'afhous:w`wv' Asst Flag:Primary Residence"
label values h`wv'afhous assflag

*wave 2 household-level value of primary residence
generate double hh`wv'ahous  = hh`wv'ahome
label variable hh`wv'ahous "hh`wv'ahous:w`wv' Assets:Primary Residence"

*wave 2 household-level flag for value of primary residence
generate byte   hh`wv'afhous = hh`wv'afhome
label variable hh`wv'afhous "hh`wv'afhous:w`wv' Asst Flag:Primary Residence"
label values hh`wv'afhous assflag

drop h`wv'ahome h`wv'afhome hh`wv'ahome hh`wv'afhome

***value of mortage
*wave 2 couple-level value of mortgage
label variable h`wv'amort "h`wv'amort:w`wv' Assets:Mortgage"

*wave 2 couple-level flag for value of mortgage
label variable h`wv'afmort "h`wv'afmort:w`wv' Asst Flag:Mortgage"

*wave 2 household-level value of mortgage
label variable hh`wv'amort "hh`wv'amort:w`wv' Assets:Mortgage"

*wave 2 household-level flag for value of mortgage
label variable hh`wv'afmort "hh`wv'afmort:w`wv' Asst Flag:Mortgage"

// hmln : not in SHARE

drop cplxhh multihouse

//--------------------------------------------------------------------
// Summary variables
//--------------------------------------------------------------------

generate double h`wv'atoth = h`wv'ahous - h`wv'amort
label variable h`wv'atoth "h`wv'atoth:w`wv' Net Value of House /prim res"

h_imputation_flag h`wv'afhous h`wv'afmort, gen(h`wv'aftoth)
label variable h`wv'aftoth "h`wv'aftoth:w`wv' Asst Flag:Net Value of House /prim res"
label values h`wv'aftoth assflag

generate double hh`wv'atoth = hh`wv'ahous - hh`wv'amort
label variable hh`wv'atoth  "hh`wv'atoth:w`wv' Net Value of House /prim res"

h_imputation_flag hh`wv'afhous hh`wv'afmort, gen(hh`wv'aftoth)
label variable hh`wv'aftoth "hh`wv'aftoth:w`wv' Asst Flag:Net Value of House /prim res"
label values hh`wv'aftoth assflag

// houb : not in SHARE
// mrtb : not in SHARE
// nethb : not in SHARE

// totf : not possible with imputations as ira wealth is tied to other long-term savings

generate double h`wv'atotb = h`wv'ahous + h`wv'arles + h`wv'atran + h`wv'absns + h`wv'airothr ///
    + h`wv'abdstk + h`wv'achck - h`wv'adebt - h`wv'amort
label variable h`wv'atotb "h`wv'atotb:w`wv' total of all assets inc. 2nd hm"

h_imputation_flag h`wv'afhous h`wv'afrles h`wv'aftran h`wv'afbsns h`wv'afirothr h`wv'afbdstk ///
                  h`wv'afchck h`wv'afdebt h`wv'afmort, gen(h`wv'aftotb)
label variable h`wv'aftotb "h`wv'aftotb:w`wv' Asst Flag:total of all assets inc. 2nd hm"
label values h`wv'aftotb assflag

generate double hh`wv'atotb = hh`wv'ahous + hh`wv'arles + hh`wv'atran + hh`wv'absns + hh`wv'airothr ///
    + hh`wv'abdstk + hh`wv'achck - hh`wv'adebt - hh`wv'amort
label variable hh`wv'atotb "hh`wv'atotb:w`wv' total of all assets inc. 2nd hm"

h_imputation_flag hh`wv'afhous hh`wv'afrles hh`wv'aftran hh`wv'afbsns hh`wv'afirothr hh`wv'afbdstk ///
                  hh`wv'afchck hh`wv'afdebt hh`wv'afmort, gen(hh`wv'aftotb)
label variable hh`wv'aftotb "hh`wv'aftotb:w`wv' Asst Flag:total of all assets inc. 2nd hm"
label values hh`wv'aftotb assflag

// totb : not relevant for SHARE
// totw : not possible with imputations as ira wealth is tied to other long-term savings

generate double h`wv'atotn = h`wv'arles + h`wv'atran + h`wv'absns + h`wv'airothr + h`wv'abdstk + ///
                             h`wv'achck - h`wv'adebt
label variable h`wv'atotn "h`wv'atotn:w`wv' Total Non-Housing Assets"

h_imputation_flag h`wv'afrles h`wv'aftran h`wv'afbsns h`wv'afirothr h`wv'afbdstk ///
                  h`wv'afchck h`wv'afdebt, gen(h`wv'aftotn)
label variable h`wv'aftotn "h`wv'aftotn:w`wv' Asst Flag:Total Non-Housing Assets"
label values h`wv'aftotn assflag

generate double hh`wv'atotn = hh`wv'arles + hh`wv'atran + hh`wv'absns + hh`wv'airothr + hh`wv'abdstk + ///
                              hh`wv'achck - hh`wv'adebt
label variable hh`wv'atotn "hh`wv'atotn:w`wv' Total Non-Housing Assets"

h_imputation_flag hh`wv'afrles hh`wv'aftran hh`wv'afbsns hh`wv'afirothr hh`wv'afbdstk ///
                  hh`wv'afchck hh`wv'afdebt, gen(hh`wv'aftotn)
label variable hh`wv'aftotn "hh`wv'aftotn:w`wv' Asst Flag:Total Non-Housing Assets"
label values hh`wv'aftotn assflag


***drop wave 2 imputation file raw variables***
drop `asset_w2_imput'

***drop wave 2 household raw variables***
drop `asset_w2_ho'

******************************************************************************************

*set wave number
local wv = 4
local pre_wv = 2

***merge with wave 4 household data***
local asset_w4_ho ho002_ ho044_
merge 1:1 mergeid using "$wave_4_ho", keepusing(`asset_w4_ho') nogen

***merge with wave 4 imputation data***
local asset_w4_imput vbus   sbus   bacc   bsmf   car   slti   liab    ///
                     vbus_f sbus_f bacc_f bsmf_f car_f slti_f liab_f  ///
                     ypen1   ypen2   ydip   yind ///
                     ypen1_f ypen2_f ydip_f yind_f ///
                     home   mort  ores ores_f  ///
                     home_f mort_f otrf otrf_f perho perho_f ///
                     p_nrp
merge 1:1 mergeid using "$wave_4_imputations_", keepusing(`asset_w4_imput') nogen


***create indicator of multiple couples in household
egen cplxhh = tag(hhid4 h4coupid)
egen multihouse = total(cplxhh) if inw4==1, by(hhid4)
recode multihouse (1=0) (2/10 = 1)

//--------------------------------------------------------------------
// Assets
//--------------------------------------------------------------------

// AS section questions are asked to the all finacial respondents on 
// behalf of the respondent and the spouse, if any spouse. Imputations
// for the respondent and spouse are summed to produce one value for all 
// members of the couple.

***create value of share of own business***
generate double ownvbus = vbus*(sbus/100)
label variable ownvbus "value of own share of businesses"

***combine imputation flags for individual business ownership***
generate byte ownvbus_f = .
replace ownvbus_f = 2 if vbus_f == 2 | sbus_f == 2
replace ownvbus_f = 3 if vbus_f == 3 | sbus_f == 3
replace ownvbus_f = 4 if vbus_f == 4 | sbus_f == 4
replace ownvbus_f = 5 if vbus_f == 5 | sbus_f == 5
replace ownvbus_f = 6 if vbus_f == 6 | sbus_f == 6
replace ownvbus_f = 7 if vbus_f == 7 | sbus_f == 7
replace ownvbus_f = 9 if vbus_f == 9 | sbus_f == 9
replace ownvbus_f = 10 if vbus_f == 10 | sbus_f == 10
replace ownvbus_f = 11 if vbus_f == 11 | sbus_f == 11
replace ownvbus_f = 8 if vbus_f == 8 | sbus_f == 8
replace ownvbus_f = 1 if vbus_f == 1 | sbus_f == 1
label variable ownvbus_f "value of own share of businesses - imputation flag"
label values ownvbus_f IMPflag

// asset categories
local ascateg ownvbus bacc bsmf car slti liab 

***create couple-level asset variables
foreach cat in `ascateg' {
    generate double h4a`cat' = `cat'
    label variable h4a`cat' "h4a`cat':w4 asset value `cat' (cpl level)"
    imputation_flag `cat'_f, gen(h4af`cat') by(h4coupid)
    label variable h4af`cat' "h4af`cat':w4 asset impu flag `cat' (cpl level)"
    label values h4af`cat' assflag
}

***create household-level household variables
foreach cat in `ascateg' {
    egen double hh4a`cat' = total(`cat' * (cplxhh==1)) if !mi(hh`wv'hhid), by(hh`wv'hhid)
    label variable hh4a`cat' "hh4a`cat':w4 asset value `cat' (hh level)"
    
    imputation_flag `cat'_f, gen(hh4af`cat') by(hh`wv'hhid)
    label variable hh4af`cat' "hh4af`cat':w4 asset impu flag `cat' (hh level)"
    label values hh4af`cat' assflag
}

drop ownvbus ownvbus_f

***value of cars
*wave 4 couple-level value of cars
generate double h4atran  = h4acar
label variable h4atran  "h4atran:w4 Assets:Cars"

*wave 4 couple-level flag for value of cars
generate byte   h4aftran = h4afcar
label variable h4aftran "h4aftran:w4 Asst Flag:Cars"
label values h4aftran assflag

drop h4acar h4afcar

*wave 4 household-level value of cars
generate double hh4atran  = hh4acar
label variable hh4atran  "hh4atran:w4 Assets:Cars"

*wave 4 household-level flag for value of cars
generate byte   hh4aftran = hh4afcar
label variable hh4aftran "hh4aftran:w4 Asst Flag:Cars"
label values hh4aftran assflag

drop hh4acar hh4afcar

***value of business
*wave 4 couple-level value of business
generate double h4absns  = h4aownvbus
label variable h4absns  "h4absns:w4 Assets:Business"

*wave 4 couple-level flag for value of business
generate byte   h4afbsns = h4afownvbus
label variable h4afbsns "h4afbsns:w4 Asst Flag:Business"
label values h4afbsns assflag

drop h4aownvbus h4afownvbus

*wave 4 household-level value of business
generate double hh4absns  = hh4aownvbus
label variable hh4absns  "hh4absns:w4 Assets:Business"

*wave 4 household-level flag for value of business
generate byte   hh4afbsns = hh4afownvbus
label variable hh4afbsns "hh4afbsns:w4 Asst Flag:Business"
label values hh4afbsns assflag

drop hh4aownvbus hh4afownvbus

***value of IRAs and other long-term savings
*wave 4 couple-level value of IRAs and other long-term savings
generate double h4airothr  = h4aslti
label variable h4airothr  "h4airothr:w4 Assets: IRAs and other long-term savings"

*wave 4 couple-level flag for value of IRAs and other long-term savings
generate byte   h4afirothr = h4afslti
label variable h4afirothr "h4afirothr:w4 Asst Flag:IRAs and other long-term savings"
label values h4afirothr assflag

drop h4aslti h4afslti

*wave 4 household-level value of IRAs and other long-term savings
generate double hh4airothr  = hh4aslti
label variable hh4airothr  "hh4airothr:w4 Assets: IRAs and other long-term savings"

*wave 4 household-level flag for value of IRAs and other long-term savings
generate byte   hh4afirothr = hh4afslti
label variable hh4afirothr "hh4afirothr:w4 Asst Flag:IRAs and other long-term savings"
label values hh4afirothr assflag

drop hh4aslti hh4afslti

***value of bonds and stocks
*wave 4 couple-level value of bonds and stocks
generate double h4abdstk  = h4absmf
label variable h4abdstk  "h4abdstk:w4 Assets:Bonds and Stocks"

*wave 4 couple-level flag for value of bonds and stocks
generate byte   h4afbdstk = h4afbsmf
label variable h4afbdstk "h4afbdstk:w4 Asst Flag:Bonds and Stocks"
label values h4afbdstk assflag

drop h4absmf h4afbsmf

*wave 4 household-level value of bonds and stocks
generate double hh4abdstk  = hh4absmf
label variable hh4abdstk  "hh4abdstk:w4 Assets:Bonds and Stocks"

*wave 4 household-level flag for value of bonds and stocks
generate byte   hh4afbdstk = hh4afbsmf
label variable hh4afbdstk "hh4afbdstk:w4 Asst Flag:Bonds and Stocks"
label values hh4afbdstk assflag

drop hh4absmf hh4afbsmf

***value of checking, savings acct
*wave 4 couple-level value of checking, savings acct
generate double h4achck  = h4abacc
label variable h4achck  "h4achck:w4 Assets:Checking,Savings Acct"

*wave 4 couple-level flag for value of checking, savings acct
generate byte   h4afchck = h4afbacc
label variable h4afchck "h4afchck:w4 Asst Flag:Checking,Savings Acct"
label values h4afchck assflag

drop h4abacc h4afbacc

*wave 4 household-level value of checking, savings acct
generate double hh4achck  = hh4abacc
label variable hh4achck  "hh4achck:w4 Assets:Checking,Savings Acct"

*wave 4 household-level flag for value of checking, savings acct
generate byte   hh4afchck = hh4afbacc
label variable hh4afchck "hh4afchck:w4 Asst Flag:Checking,Savings Acct"
label values hh4afchck assflag

drop hh4abacc hh4afbacc

***value of debts
*wave 4 couple-level value of debts
generate double h4adebt  = h4aliab
label variable h4adebt  "h4adebt:w4 Assets:Debts"

*wave 4 couple-level flag for value of debts
generate byte   h4afdebt = h4afliab
label variable h4afdebt "h4afdebt:w4 Asst Flag:Debts"
label values h4afdebt assflag

drop h4aliab h4afliab

*wave 4 household-level value of debts
generate double hh4adebt  = hh4aliab
label variable hh4adebt  "hh4adebt:w4 Assets:Debts"

*wave 4 household-level flag for value of debts
generate byte   hh4afdebt = hh4afliab
label variable hh4afdebt "hh4afdebt:w4 Asst Flag:Debts"
label values hh4afdebt assflag

drop hh4aliab hh4afliab

// HO section questions are asked to all household respondents on behalf of 
// the whole household. Even though multiple respondents can complete
// the household section the imputations for these questions are only based
// one of those respondents answers and all members of the household
// are given the same value. Given this couple-level
// variables can only be derived for couples without any additional household
// members. Household-level variables are derived for all respondents.

*generate couple-level value of other real estate variables
generate double h4aores = ores if multihouse == 0
label variable h4aores "h4aores:w4 asset value ores (cpl level)"

imputation_flag ores_f if multihouse == 0, gen(h4afores) by(hh`wv'hhid)
replace h4afores = -1 if multihouse == 1
label variable h4afores "h4afores:w4 asset impu flag ores (cpl level)"
label values h4afores assflag

*generate household-level value of other real estate variables
generate double hh4aores = ores
label variable hh4aores "hh4aores:w4 asset value ores (hh level)"

imputation_flag ores_f, gen(hh4afores) by(hh`wv'hhid)
label variable hh4afores "hh4afores:w4 asset impu flag ores (hh level)"
label values hh4afores assflag

*create more complete imputation variable for value of primary residence
gen home_hf = home_f
replace home_hf = 2 if inlist(otrf,3,4,5) & otrf_f == 3
replace home_hf = 8 if otrf_f == 14

*generate couple-level value of primary residence variables
generate double h4ahome = home if multihouse == 0
label variable h4ahome "h4ahome:w4 asset value home (cpl level)"

imputation_flag home_hf if multihouse == 0, gen(h4afhome) by(hh`wv'hhid)
replace h4afhome = -1 if multihouse == 1
label variable h4afhome "h4afhome:w4 asset impu flag home (cpl level)"
label values h4afhome assflag

*generate household-level value of primary residence variables
generate double hh4ahome = home
label variable hh4ahome "hh4ahome:w4 asset value home (hh level)"

imputation_flag home_hf, gen(hh4afhome) by(hh`wv'hhid)
label variable hh4afhome "hh4afhome:w4 asset impu flag home (hh level)"
label values hh4afhome assflag
drop home_hf

*create more complete imputation variable for value of mortgage
gen mort_hf = mort_f
replace mort_hf = 2 if inlist(otrf,3,4,5) & otrf_f == 3
replace mort_hf = 2 if perho == 0 & perho_f == 3

*generate couple-level value of mortgage variables
generate double h4amort = mort if multihouse == 0
label variable h4amort "h4amort:w4 asset value mort (cpl level)"

imputation_flag mort_hf if multihouse == 0, gen(h4afmort) by(hh`wv'hhid)
replace h4afmort = -1 if multihouse == 1
label variable h4afmort "h4afmort:w4 asset impu flag mort (cpl level)"
label values h4afmort assflag

*generate household-level value of mortage variables
generate double hh4amort = mort
label variable hh4amort "hh4amort:w4 asset value mort (hh level)"

imputation_flag mort_hf, gen(hh4afmort) by(hh`wv'hhid)
label variable hh4afmort "hh4afmort:w4 asset impu flag mort (hh level)"
label values hh4afmort assflag
drop mort_hf

***home ownership***
*wave 4 couple-level home ownership
gen h`wv'ahown = .
replace h`wv'ahown = .m if ho002_ == . & inw`wv' == 1
replace h`wv'ahown = .d if ho002_ == -1
replace h`wv'ahown = .r if ho002_ == -2
replace h`wv'ahown = h`pre_wv'ahown if inrange(h`pre_wv'ahown,0,1) & ho044_ == 5
replace h`wv'ahown = 0 if inlist(ho002_,2,3,4,5,6)
replace h`wv'ahown = 1 if ho002_ == 1
bysort h`wv'coupid: replace h`wv'ahown = h`wv'ahown[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'ahown = h`wv'ahown[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse
replace h`wv'ahown = .m if h`wv'ahown==. & inw`wv'==1
label var h`wv'ahown "h`wv'ahown:w`wv' whether own home"
label values h`wv'ahown homeowner

***value of other real estate
*wave 4 couple-level value of other real estate
generate double h4arles  = h4aores
label variable h4arles  "h4arles:w4 Assets:Other Real estate"

*wave 4 couple-level flag for value of other real estate
generate byte   h4afrles = h4afores
label variable h4afrles "h4afrles:w4 Asst Flag:Other Real estate"
label values h4afrles assflag

drop h4aores h4afores  

*wave 4 household-level value of other real estate
generate double hh4arles  = hh4aores
label variable hh4arles  "hh4arles:w4 Assets:Other Real estate"

*wave 4 household-level flag for value of other real estate
generate byte   hh4afrles = hh4afores
label variable hh4afrles "hh4afrles:w4 Asst Flag:Other Real estate"
label values hh4afrles assflag

drop hh4aores hh4afores  

***value of primary residence
*wave 4 couple-level value of primary residence
generate double h4ahous  = h4ahome
label variable h4ahous  "h4ahous:w4 Assets:Primary Residence"

*wave 4 couple-level flag for value of primary residence
generate byte   h4afhous = h4afhome
label variable h4afhous "h4afhous:w4 Asst Flag:Primary Residence"
label values h4afhous assflag

*wave 4 household-level value of primary residence
generate double hh4ahous  = hh4ahome
label variable hh4ahous  "hh4ahous:w4 Assets:Primary Residence"

*wave 4 household-level flag for value of primary residence
generate byte   hh4afhous = hh4afhome
label variable hh4afhous "hh4afhous:w4 Asst Flag:Primary Residence"
label values hh4afhous assflag

drop h4ahome h4afhome hh4ahome hh4afhome

***value of mortage
*wave 4 couple-level value of mortgage
label variable h4amort  "h4amort:w4 Assets:Mortgage"

*wave 4 couple-level flag for value of mortgage
label variable h4afmort "h4afmort:w4 Asst Flag:Mortgage"

*wave 4 household-level value of mortgage
label variable hh4amort  "hh4amort:w4 Assets:Mortgage"

*wave 4 household-level flag for value of mortgage
label variable hh4afmort "hh4afmort:w4 Asst Flag:Mortgage"

// hmln : not in SHARE

drop cplxhh multihouse

//--------------------------------------------------------------------
// Summary variables
//--------------------------------------------------------------------

generate double h`wv'atoth = h`wv'ahous - h`wv'amort
label variable h`wv'atoth "h`wv'atoth:w`wv' Net Value of House /prim res"

h_imputation_flag h`wv'afhous h`wv'afmort, gen(h`wv'aftoth)
label variable h`wv'aftoth "h`wv'aftoth:w`wv' Asst Flag:Net Value of House /prim res"
label values h`wv'aftoth assflag

generate double hh`wv'atoth = hh`wv'ahous - hh`wv'amort
label variable hh`wv'atoth  "hh`wv'atoth:w`wv' Net Value of House /prim res"

h_imputation_flag hh`wv'afhous hh`wv'afmort, gen(hh`wv'aftoth)
label variable hh`wv'aftoth "hh`wv'aftoth:w`wv' Asst Flag:Net Value of House /prim res"
label values hh`wv'aftoth assflag

// houb : not in SHARE
// mrtb : not in SHARE
// nethb : not in SHARE

// totf : not possible with imputations as ira wealth is tied to other long-term savings

generate double h`wv'atotb = h`wv'ahous + h`wv'arles + h`wv'atran + h`wv'absns + h`wv'airothr ///
    + h`wv'abdstk + h`wv'achck - h`wv'adebt - h`wv'amort
label variable h`wv'atotb "h`wv'atotb:w`wv' total of all assets inc. 2nd hm"

h_imputation_flag h`wv'afhous h`wv'afrles h`wv'aftran h`wv'afbsns h`wv'afirothr h`wv'afbdstk ///
                  h`wv'afchck h`wv'afdebt h`wv'afmort, gen(h`wv'aftotb)
label variable h`wv'aftotb "h`wv'aftotb:w`wv' Asst Flag:total of all assets inc. 2nd hm"
label values h`wv'aftotb assflag

generate double hh`wv'atotb = hh`wv'ahous + hh`wv'arles + hh`wv'atran + hh`wv'absns + hh`wv'airothr ///
    + hh`wv'abdstk + hh`wv'achck - hh`wv'adebt - hh`wv'amort
label variable hh`wv'atotb "hh`wv'atotb:w`wv' total of all assets inc. 2nd hm"

h_imputation_flag hh`wv'afhous hh`wv'afrles hh`wv'aftran hh`wv'afbsns hh`wv'afirothr hh`wv'afbdstk ///
                  hh`wv'afchck hh`wv'afdebt hh`wv'afmort, gen(hh`wv'aftotb)
label variable hh`wv'aftotb "hh`wv'aftotb:w`wv' Asst Flag:total of all assets inc. 2nd hm"
label values hh`wv'aftotb assflag

// totb : not relevant for SHARE
// totw : not possible with imputations as ira wealth is tied to other long-term savings

generate double h`wv'atotn = h`wv'arles + h`wv'atran + h`wv'absns + h`wv'airothr + h`wv'abdstk + ///
                             h`wv'achck - h`wv'adebt
label variable h`wv'atotn "h`wv'atotn:w`wv' Total Non-Housing Assets"

h_imputation_flag h`wv'afrles h`wv'aftran h`wv'afbsns h`wv'afirothr h`wv'afbdstk ///
                  h`wv'afchck h`wv'afdebt, gen(h`wv'aftotn)
label variable h`wv'aftotn "h`wv'aftotn:w`wv' Asst Flag:Total Non-Housing Assets"
label values h`wv'aftotn assflag

generate double hh`wv'atotn = hh`wv'arles + hh`wv'atran + hh`wv'absns + hh`wv'airothr + hh`wv'abdstk + ///
                              hh`wv'achck - hh`wv'adebt
label variable hh`wv'atotn "hh`wv'atotn:w`wv' Total Non-Housing Assets"

h_imputation_flag hh`wv'afrles hh`wv'aftran hh`wv'afbsns hh`wv'afirothr hh`wv'afbdstk ///
                  hh`wv'afchck hh`wv'afdebt, gen(hh`wv'aftotn)
label variable hh`wv'aftotn "hh`wv'aftotn:w`wv' Asst Flag:Total Non-Housing Assets"
label values hh`wv'aftotn assflag


***drop wave 4 imputation file raw variables***
drop `asset_w4_imput'

***drop wave 4 household raw variables***
drop `asset_w4_ho'

******************************************************************************************

*set wave number
local wv = 5
local pre_wv = 4

***merge with wave 5 household data***
local asset_w5_ho ho002_ ho044_ 
merge 1:1 mergeid using "$wave_5_ho", keepusing(`asset_w5_ho') nogen

***merge with wave 5 imputation data***
local asset_w5_imput vbus   sbus   bacc   bsmf   car   slti   liab    ///
                     vbus_f sbus_f bacc_f bsmf_f car_f slti_f liab_f  ///
                     ypen1   ypen2   ydip   yind ///
                     ypen1_f ypen2_f ydip_f yind_f ///
                     home   mort  ores ores_f  ///
                     home_f mort_f otrf otrf_f perho perho_f ///
                     p_nrp
merge 1:1 mergeid using "$wave_5_imputations_", keepusing(`asset_w5_imput') nogen


***create indicator of multiple couples in household
egen cplxhh = tag(hhid5 h5coupid)
egen multihouse = total(cplxhh) if inw5==1, by(hhid5)
recode multihouse (1=0) (2/10 = 1)

//--------------------------------------------------------------------
// Assets
//--------------------------------------------------------------------

// AS section questions are asked to the all finacial respondents on 
// behalf of the respondent and the spouse, if any spouse. Imputations
// for the respondent and spouse are summed to produce one value for all 
// members of the couple.

***create value of share of own business***
generate double ownvbus = vbus*(sbus/100)
label variable ownvbus "value of own share of businesses"

***combine imputation flags for individual business ownership***
generate byte ownvbus_f = .
replace ownvbus_f = 2 if vbus_f == 2 | sbus_f == 2
replace ownvbus_f = 3 if vbus_f == 3 | sbus_f == 3
replace ownvbus_f = 4 if vbus_f == 4 | sbus_f == 4
replace ownvbus_f = 5 if vbus_f == 5 | sbus_f == 5
replace ownvbus_f = 6 if vbus_f == 6 | sbus_f == 6
replace ownvbus_f = 7 if vbus_f == 7 | sbus_f == 7
replace ownvbus_f = 9 if vbus_f == 9 | sbus_f == 9
replace ownvbus_f = 10 if vbus_f == 10 | sbus_f == 10
replace ownvbus_f = 11 if vbus_f == 11 | sbus_f == 11
replace ownvbus_f = 8 if vbus_f == 8 | sbus_f == 8
replace ownvbus_f = 1 if vbus_f == 1 | sbus_f == 1
label variable ownvbus_f "value of own share of businesses - imputation flag"
label values ownvbus_f IMPflag

// asset categories
local ascateg ownvbus bacc bsmf car slti liab 

***create couple-level asset variables
foreach cat in `ascateg' {
    generate double h5a`cat' = `cat'
    label variable h5a`cat' "h5a`cat':w5 asset value `cat' (cpl level)"
    imputation_flag `cat'_f, gen(h5af`cat') by(h5coupid)
    label variable h5af`cat' "h5af`cat':w5 asset impu flag `cat' (cpl level)"
    label values h5af`cat' assflag
}

***create household-level household variables
foreach cat in `ascateg' {
    egen double hh5a`cat' = total(`cat' * (cplxhh==1)) if !mi(hhid5), by(hh`wv'hhid)
    label variable hh5a`cat' "hh5a`cat':w5 asset value `cat' (hh level)"
    
    imputation_flag `cat'_f, gen(hh5af`cat') by(hh`wv'hhid)
    label variable hh5af`cat' "hh5af`cat':w5 asset impu flag `cat' (hh level)"
    label values hh5af`cat' assflag
}

drop ownvbus ownvbus_f

***value of cars
*wave 5 couple-level value of cars
generate double h5atran  = h5acar
label variable h5atran "h5atran:w5 Assets:Cars"

*wave 5 couple-level flag for value of cars
generate byte   h5aftran = h5afcar
label variable h5aftran "h5aftran:w5 Asst Flag:Cars"
label values h5aftran assflag

drop h5acar h5afcar

*wave 5 household-level value of cars
generate double hh5atran  = hh5acar
label variable hh5atran "hh5atran:w5 Assets:Cars"

*wave 5 household-level flag for value of cars
generate byte   hh5aftran = hh5afcar
label variable hh5aftran "hh5aftran:w5 Asst Flag:Cars"
label values hh5aftran assflag

drop hh5acar hh5afcar

***value of business
*wave 5 couple-level value of business
generate double h5absns  = h5aownvbus
label variable h5absns "h5absns:w5 Assets:Business"

*wave 5 couple-level flag for value of business
generate byte   h5afbsns = h5afownvbus
label variable h5afbsns "h5afbsns:w5 Asst Flag:Business"
label values h5afbsns assflag

drop h5aownvbus h5afownvbus

*wave 5 household-level value of business
generate double hh5absns  = hh5aownvbus
label variable hh5absns "hh5absns:w5 Assets:Business"

*wave 5 household-level flag for value of business
generate byte   hh5afbsns = hh5afownvbus
label variable hh5afbsns "hh5afbsns:w5 Asst Flag:Business"
label values hh5afbsns assflag

drop hh5aownvbus hh5afownvbus

***value of IRAs and other long-term savings
*wave 5 couple-level value of IRAs and other long-term savings
generate double h5airothr  = h5aslti
label variable h5airothr "h5airothr:w5 Assets: IRAs and other long-term savings"

*wave 5 couple-level flag for value of IRAs and other long-term savings
generate byte   h5afirothr = h5afslti
label variable h5afirothr "h5afirothr:w5 Asst Flag:IRAs and other long-term savings"
label values h5afirothr assflag

drop h5aslti h5afslti

*wave 5 household-level value of IRAs and other long-term savings
generate double hh5airothr  = hh5aslti
label variable hh5airothr "hh5airothr:w5 Assets: IRAs and other long-term savings"

*wave 5 household-level flag for value of IRAs and other long-term savings
generate byte   hh5afirothr = hh5afslti
label variable hh5afirothr "hh5afirothr:w5 Asst Flag:IRAs and other long-term savings"
label values hh5afirothr assflag

drop hh5aslti hh5afslti

***value of bonds and stocks
*wave 5 couple-level value of bonds and stocks
generate double h5abdstk  = h5absmf
label variable h5abdstk "h5abdstk:w5 Assets:Bonds and Stocks"

*wave 5 couple-level flag for value of bonds and stocks
generate byte   h5afbdstk = h5afbsmf
label variable h5afbdstk "h5afbdstk:w5 Asst Flag:Bonds and Stocks"
label values h5afbdstk assflag

drop h5absmf h5afbsmf

*wave 5 household-level value of bonds and stocks
generate double hh5abdstk  = hh5absmf
label variable hh5abdstk "hh5abdstk:w5 Assets:Bonds and Stocks"

*wave 5 household-level flag for value of bonds and stocks
generate byte   hh5afbdstk = hh5afbsmf
label variable hh5afbdstk "hh5afbdstk:w5 Asst Flag:Bonds and Stocks"
label values hh5afbdstk assflag

drop hh5absmf hh5afbsmf

***value of checking, savings acct
*wave 5 couple-level value of checking, savings acct
generate double h5achck  = h5abacc
label variable h5achck "h5achck:w5 Assets:Checking,Savings Acct"

*wave 5 couple-level flag for value of checking, savings acct
generate byte   h5afchck = h5afbacc
label variable h5afchck "h5afchck:w5 Asst Flag:Checking,Savings Acct"
label values h5afchck assflag

drop h5abacc h5afbacc

*wave 5 household-level value of checking, savings acct
generate double hh5achck  = hh5abacc
label variable hh5achck "hh5achck:w5 Assets:Checking,Savings Acct"

*wave 5 household-level flag for value of checking, savings acct
generate byte   hh5afchck = hh5afbacc
label variable hh5afchck "hh5afchck:w5 Asst Flag:Checking,Savings Acct"
label values hh5afchck assflag

drop hh5abacc hh5afbacc

***value of debts
*wave 5 couple-level value of debts
generate double h5adebt  = h5aliab
label variable h5adebt "h5adebt:w5 Assets:Debts"

*wave 5 couple-level flag for value of debts
generate byte   h5afdebt = h5afliab
label variable h5afdebt "h5afdebt:w5 Asst Flag:Debts"
label values h5afdebt assflag

drop h5aliab h5afliab

*wave 5 household-level value of debts
generate double hh5adebt  = hh5aliab
label variable hh5adebt "hh5adebt:w5 Assets:Debts"

*wave 5 household-level flag for value of debts
generate byte   hh5afdebt = hh5afliab
label variable hh5afdebt "hh5afdebt:w5 Asst Flag:Debts"
label values hh5afdebt assflag

drop hh5aliab hh5afliab

// HO section questions are asked to all household respondents on behalf of 
// the whole household. Even though multiple respondents can complete
// the household section the imputations for these questions are only based
// one of those respondents answers and all members of the household
// are given the same value. Given this couple-level
// variables can only be derived for couples without any additional household
// members. Household-level variables are derived for all respondents.

*generate couple-level value of other real estate variables
generate double h5aores = ores if multihouse == 0
label variable h5aores "h5aores:w5 asset value ores (cpl level)"

imputation_flag ores_f if multihouse == 0, gen(h5afores) by(hh`wv'hhid)
replace h5afores = -1 if multihouse == 1
label variable h5afores "h5afores:w5 asset impu flag ores (cpl level)"
label values h5afores assflag

*generate household-level value of other real estate variables
generate double hh5aores = ores
label variable hh5aores "hh5aores:w5 asset value ores (hh level)"

imputation_flag ores_f, gen(hh5afores) by(hh`wv'hhid)
label variable hh5afores "hh5afores:w5 asset impu flag ores (hh level)"
label values hh5afores assflag

*create more complete imputation variable for value of primary residence
gen home_hf = home_f
replace home_hf = 2 if inlist(otrf,3,4,5) & otrf_f == 3
replace home_hf = 8 if otrf_f == 14

*generate couple-level value of primary residence variables
generate double h5ahome = home if multihouse == 0
label variable h5ahome "h5ahome:w5 asset value home (cpl level)"

imputation_flag home_hf if multihouse == 0, gen(h5afhome) by(hh`wv'hhid)
replace h5afhome = -1 if multihouse == 1
label variable h5afhome "h5afhome:w5 asset impu flag home (cpl level)"
label values h5afhome assflag

*generate household-level value of primary residence variables
generate double hh5ahome = home
label variable hh5ahome "hh5ahome:w5 asset value home (hh level)"

imputation_flag home_hf, gen(hh5afhome) by(hh`wv'hhid)
label variable hh5afhome "hh5afhome:w5 asset impu flag home (hh level)"
label values hh5afhome assflag
drop home_hf

*create more complete imputation variable for value of mortgage
gen mort_hf = mort_f
replace mort_hf = 2 if inlist(otrf,3,4,5) & otrf_f == 3
replace mort_hf = 2 if perho == 0 & perho_f == 3

*generate couple-level value of mortgage variables
generate double h5amort = mort if multihouse == 0
label variable h5amort "h5amort:w5 asset value mort (cpl level)"

imputation_flag mort_hf if multihouse == 0, gen(h5afmort) by(hh`wv'hhid)
replace h5afmort = -1 if multihouse == 1
label variable h5afmort "h5afmort:w5 asset impu flag mort (cpl level)"
label values h5afmort assflag

*generate household-level value of mortage variables
generate double hh5amort = mort
label variable hh5amort "hh5amort:w5 asset value mort (hh level)"

imputation_flag mort_hf, gen(hh5afmort) by(hh`wv'hhid)
label variable hh5afmort "hh5afmort:w5 asset impu flag mort (hh level)"
label values hh5afmort assflag
drop mort_hf

***home ownership***
*wave 5 couple-level home ownership
gen h`wv'ahown = .
replace h`wv'ahown = .m if ho002_ == . & inw`wv' == 1
replace h`wv'ahown = .d if ho002_ == -1
replace h`wv'ahown = .r if ho002_ == -2
replace h`wv'ahown = h`pre_wv'ahown if inrange(h`pre_wv'ahown,0,1) & ho044_ == 5
replace h`wv'ahown = 0 if inlist(ho002_,2,3,4,5,6)
replace h`wv'ahown = 1 if ho002_ == 1
bysort h`wv'coupid: replace h`wv'ahown = h`wv'ahown[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'ahown = h`wv'ahown[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse
replace h`wv'ahown = .m if h`wv'ahown==. & inw`wv'==1
label var h`wv'ahown "h`wv'ahown:w`wv' whether own home"
label values h`wv'ahown homeowner

***value of other real estate
*wave 5 couple-level value of other real estate
generate double h5arles  = h5aores
label variable h5arles "h5arles:w5 Assets:Other Real estate"

*wave 5 couple-level flag for value of other real estate
generate byte   h5afrles = h5afores
label variable h5afrles "h5afrles:w5 Asst Flag:Other Real estate"
label values h5afrles assflag

drop h5aores h5afores  

*wave 5 household-level value of other real estate
generate double hh5arles  = hh5aores
label variable hh5arles "hh5arles:w5 Assets:Other Real estate"

*wave 5 household-level flag for value of other real estate
generate byte   hh5afrles = hh5afores
label variable hh5afrles "hh5afrles:w5 Asst Flag:Other Real estate"
label values hh5afrles assflag

drop hh5aores hh5afores  

***value of primary residence
*wave 5 couple-level value of primary residence
generate double h5ahous  = h5ahome
label variable h5ahous "h5ahous:w5 Assets:Primary Residence"

*wave 5 couple-level flag for value of primary residence
generate byte   h5afhous = h5afhome
label variable h5afhous "h5afhous:w5 Asst Flag:Primary Residence"
label values h5afhous assflag

*wave 5 household-level value of primary residence
generate double hh5ahous  = hh5ahome
label variable hh5ahous "hh5ahous:w5 Assets:Primary Residence"

*wave 5 household-level flag for value of primary residence
generate byte   hh5afhous = hh5afhome
label variable hh5afhous "hh5afhous:w5 Asst Flag:Primary Residence"
label values hh5afhous assflag

drop h5ahome h5afhome hh5ahome hh5afhome

***value of mortage
*wave 5 couple-level value of mortgage
label variable h5amort "h5amort:w5 Assets:Mortgage"

*wave 5 couple-level flag for value of mortgage
label variable h5afmort "h5afmort:w5 Asst Flag:Mortgage"

*wave 5 household-level value of mortgage
label variable hh5amort "hh5amort:w5 Assets:Mortgage"

*wave 5 household-level flag for value of mortgage
label variable hh5afmort "hh5afmort:w5 Asst Flag:Mortgage"

// hmln : not in SHARE

drop cplxhh multihouse

//--------------------------------------------------------------------
// Summary variables
//--------------------------------------------------------------------

generate double h`wv'atoth = h`wv'ahous - h`wv'amort
label variable h`wv'atoth "h`wv'atoth:w`wv' Net Value of House /prim res"

h_imputation_flag h`wv'afhous h`wv'afmort, gen(h`wv'aftoth)
label variable h`wv'aftoth "h`wv'aftoth:w`wv' Asst Flag:Net Value of House /prim res"
label values h`wv'aftoth assflag

generate double hh`wv'atoth = hh`wv'ahous - hh`wv'amort
label variable hh`wv'atoth  "hh`wv'atoth:w`wv' Net Value of House /prim res"

h_imputation_flag hh`wv'afhous hh`wv'afmort, gen(hh`wv'aftoth)
label variable hh`wv'aftoth "hh`wv'aftoth:w`wv' Asst Flag:Net Value of House /prim res"
label values hh`wv'aftoth assflag

// houb : not in SHARE
// mrtb : not in SHARE
// nethb : not in SHARE

// totf : not possible with imputations as ira wealth is tied to other long-term savings

generate double h`wv'atotb = h`wv'ahous + h`wv'arles + h`wv'atran + h`wv'absns + h`wv'airothr ///
    + h`wv'abdstk + h`wv'achck - h`wv'adebt - h`wv'amort
label variable h`wv'atotb "h`wv'atotb:w`wv' total of all assets inc. 2nd hm"

h_imputation_flag h`wv'afhous h`wv'afrles h`wv'aftran h`wv'afbsns h`wv'afirothr h`wv'afbdstk ///
                  h`wv'afchck h`wv'afdebt h`wv'afmort, gen(h`wv'aftotb)
label variable h`wv'aftotb "h`wv'aftotb:w`wv' Asst Flag:total of all assets inc. 2nd hm"
label values h`wv'aftotb assflag

generate double hh`wv'atotb = hh`wv'ahous + hh`wv'arles + hh`wv'atran + hh`wv'absns + hh`wv'airothr ///
    + hh`wv'abdstk + hh`wv'achck - hh`wv'adebt - hh`wv'amort
label variable hh`wv'atotb "hh`wv'atotb:w`wv' total of all assets inc. 2nd hm"

h_imputation_flag hh`wv'afhous hh`wv'afrles hh`wv'aftran hh`wv'afbsns hh`wv'afirothr hh`wv'afbdstk ///
                  hh`wv'afchck hh`wv'afdebt hh`wv'afmort, gen(hh`wv'aftotb)
label variable hh`wv'aftotb "hh`wv'aftotb:w`wv' Asst Flag:total of all assets inc. 2nd hm"
label values hh`wv'aftotb assflag

// totb : not relevant for SHARE
// totw : not possible with imputations as ira wealth is tied to other long-term savings

generate double h`wv'atotn = h`wv'arles + h`wv'atran + h`wv'absns + h`wv'airothr + h`wv'abdstk + ///
                             h`wv'achck - h`wv'adebt
label variable h`wv'atotn "h`wv'atotn:w`wv' Total Non-Housing Assets"

h_imputation_flag h`wv'afrles h`wv'aftran h`wv'afbsns h`wv'afirothr h`wv'afbdstk ///
                  h`wv'afchck h`wv'afdebt, gen(h`wv'aftotn)
label variable h`wv'aftotn "h`wv'aftotn:w`wv' Asst Flag:Total Non-Housing Assets"
label values h`wv'aftotn assflag

generate double hh`wv'atotn = hh`wv'arles + hh`wv'atran + hh`wv'absns + hh`wv'airothr + hh`wv'abdstk + ///
                              hh`wv'achck - hh`wv'adebt
label variable hh`wv'atotn "hh`wv'atotn:w`wv' Total Non-Housing Assets"

h_imputation_flag hh`wv'afrles hh`wv'aftran hh`wv'afbsns hh`wv'afirothr hh`wv'afbdstk ///
                  hh`wv'afchck hh`wv'afdebt, gen(hh`wv'aftotn)
label variable hh`wv'aftotn "hh`wv'aftotn:w`wv' Asst Flag:Total Non-Housing Assets"
label values hh`wv'aftotn assflag


***drop wave 5 imputation file raw variables***
drop `asset_w5_imput'

***drop wave 5 household raw variables***
drop `asset_w5_ho'

******************************************************************************************

*set wave number
local wv = 6
local pre_wv = 5

***merge with wave 6 household data***
local asset_w6_ho ho002_ ho044_ 
merge 1:1 mergeid using "$wave_6_ho", keepusing(`asset_w6_ho') nogen

***merge with wave 6 imputation data***
local asset_w6_imput vbus sbus bacc bsmf car slti liab  ///
                     vbus_f sbus_f bacc_f bsmf_f car_f slti_f liab_f ///
                     home  mort ores otrf perho ///
                     home_f mort_f ores_f otrf_f perho_f                      
merge 1:1 mergeid using "$wave_6_imputations_", keepusing(`asset_w6_imput') nogen


***create indicator of multiple couples in household
egen cplxhh = tag(hhid6 h6coupid)
egen multihouse = total(cplxhh) if inw6==1, by(hhid6)
recode multihouse (1=0) (2/10 = 1)

//--------------------------------------------------------------------
// Assets
//--------------------------------------------------------------------

// AS section questions are asked to the all finacial respondents on 
// behalf of the respondent and the spouse, if any spouse. Imputations
// for the respondent and spouse are summed to produce one value for all 
// members of the couple.

***create value of share of own business***
generate double ownvbus = vbus*(sbus/100)
label variable ownvbus "value of own share of businesses"

***combine imputation flags for individual business ownership***
generate byte ownvbus_f = .
replace ownvbus_f = 2 if vbus_f == 2 | sbus_f == 2
replace ownvbus_f = 3 if vbus_f == 3 | sbus_f == 3
replace ownvbus_f = 4 if vbus_f == 4 | sbus_f == 4
replace ownvbus_f = 5 if vbus_f == 5 | sbus_f == 5
replace ownvbus_f = 6 if vbus_f == 6 | sbus_f == 6
replace ownvbus_f = 7 if vbus_f == 7 | sbus_f == 7
replace ownvbus_f = 9 if vbus_f == 9 | sbus_f == 9
replace ownvbus_f = 10 if vbus_f == 10 | sbus_f == 10
replace ownvbus_f = 11 if vbus_f == 11 | sbus_f == 11
replace ownvbus_f = 8 if vbus_f == 8 | sbus_f == 8
replace ownvbus_f = 1 if vbus_f == 1 | sbus_f == 1
label variable ownvbus_f "value of own share of businesses - imputation flag"
label values ownvbus_f IMPflag

// asset categories
local ascateg ownvbus bacc bsmf car slti liab 

***create couple-level asset variables
foreach cat in `ascateg' {
    generate double h6a`cat' = `cat'
    label variable h6a`cat' "h6a`cat':w6 asset value `cat' (cpl level)"
    imputation_flag `cat'_f, gen(h6af`cat') by(h6coupid)
    label variable h6af`cat' "h6af`cat':w6 asset impu flag `cat' (cpl level)"
    label values h6af`cat' assflag
}

***create household-level household variables
foreach cat in `ascateg' {
    egen double hh6a`cat' = total(`cat' * (cplxhh==1)) if !mi(hhid6), by(hh`wv'hhid)
    label variable hh6a`cat' "hh6a`cat':w6 asset value `cat' (hh level)"
    
    imputation_flag `cat'_f, gen(hh6af`cat') by(hh`wv'hhid)
    label variable hh6af`cat' "hh6af`cat':w6 asset impu flag `cat' (hh level)"
    label values hh6af`cat' assflag
}

drop ownvbus ownvbus_f

***value of cars
*wave 6 couple-level value of cars
generate double h6atran  = h6acar
label variable h6atran "h6atran:w6 Assets:Cars"

*wave 6 couple-level flag for value of cars
generate byte h6aftran = h6afcar
label variable h6aftran "h6aftran:w6 Asst Flag:Cars"
label values h6aftran assflag

drop h6acar h6afcar

*wave 6 household-level value of cars
generate double hh6atran  = hh6acar
label variable hh6atran "hh6atran:w6 Assets:Cars"

*wave 6 household-level flag for value of cars
generate byte hh6aftran = hh6afcar
label variable hh6aftran "hh6aftran:w6 Asst Flag:Cars"
label values hh6aftran assflag

drop hh6acar hh6afcar

***value of business
*wave 6 couple-level value of business
generate double h6absns  = h6aownvbus
label variable h6absns "h6absns:w6 Assets:Business"

*wave 6 couple-level flag for value of business
generate byte h6afbsns = h6afownvbus
label variable h6afbsns "h6afbsns:w6 Asst Flag:Business"
label values h6afbsns assflag

drop h6aownvbus h6afownvbus

*wave 6 household-level value of business
generate double hh6absns  = hh6aownvbus
label variable hh6absns "hh6absns:w6 Assets:Business"

*wave 6 household-level flag for value of business
generate byte hh6afbsns = hh6afownvbus
label variable hh6afbsns "hh6afbsns:w6 Asst Flag:Business"
label values hh6afbsns assflag

drop hh6aownvbus hh6afownvbus

***value of IRAs and other long-term savings
*wave 6 couple-level value of IRAs and other long-term savings
generate double h6airothr  = h6aslti
label variable h6airothr "h6airothr:w6 Assets: IRAs and other long-term savings"

*wave 6 couple-level flag for value of IRAs and other long-term savings
generate byte h6afirothr = h6afslti
label variable h6afirothr "h6afirothr:w6 Asst Flag:IRAs and other long-term savings"
label values h6afirothr assflag

drop h6aslti h6afslti

*wave 6 household-level value of IRAs and other long-term savings
generate double hh6airothr  = hh6aslti
label variable hh6airothr "hh6airothr:w6 Assets: IRAs and other long-term savings"

*wave 6 household-level flag for value of IRAs and other long-term savings
generate byte hh6afirothr = hh6afslti
label variable hh6afirothr "hh6afirothr:w6 Asst Flag:IRAs and other long-term savings"
label values hh6afirothr assflag

drop hh6aslti hh6afslti

***value of bonds and stocks
*wave 6 couple-level value of bonds and stocks
generate double h6abdstk  = h6absmf
label variable h6abdstk "h6abdstk:w6 Assets:Bonds and Stocks"

*wave 6 couple-level flag for value of bonds and stocks
generate byte h6afbdstk = h6afbsmf
label variable h6afbdstk "h6afbdstk:w6 Asst Flag:Bonds and Stocks"
label values h6afbdstk assflag

drop h6absmf h6afbsmf

*wave 6 household-level value of bonds and stocks
generate double hh6abdstk  = hh6absmf
label variable hh6abdstk "hh6abdstk:w6 Assets:Bonds and Stocks"

*wave 6 household-level flag for value of bonds and stocks
generate byte hh6afbdstk = hh6afbsmf
label variable hh6afbdstk "hh6afbdstk:w6 Asst Flag:Bonds and Stocks"
label values hh6afbdstk assflag

drop hh6absmf hh6afbsmf

***value of checking, savings acct
*wave 6 couple-level value of checking, savings acct
generate double h6achck  = h6abacc
label variable h6achck "h6achck:w6 Assets:Checking,Savings Acct"

*wave 6 couple-level flag for value of checking, savings acct
generate byte h6afchck = h6afbacc
label variable h6afchck "h6afchck:w6 Asst Flag:Checking,Savings Acct"
label values h6afchck assflag

drop h6abacc h6afbacc

*wave 6 household-level value of checking, savings acct
generate double hh6achck  = hh6abacc
label variable hh6achck "hh6achck:w6 Assets:Checking,Savings Acct"

*wave 6 household-level flag for value of checking, savings acct
generate byte hh6afchck = hh6afbacc
label variable hh6afchck "hh6afchck:w6 Asst Flag:Checking,Savings Acct"
label values hh6afchck assflag

drop hh6abacc hh6afbacc

***value of debts
*wave 6 couple-level value of debts
generate double h6adebt  = h6aliab
label variable h6adebt "h6adebt:w6 Assets:Debts"

*wave 6 couple-level flag for value of debts
generate byte h6afdebt = h6afliab
label variable h6afdebt "h6afdebt:w6 Asst Flag:Debts"
label values h6afdebt assflag

drop h6aliab h6afliab

*wave 6 household-level value of debts
generate double hh6adebt  = hh6aliab
label variable hh6adebt "hh6adebt:w6 Assets:Debts"

*wave 6 household-level flag for value of debts
generate byte hh6afdebt = hh6afliab
label variable hh6afdebt "hh6afdebt:w6 Asst Flag:Debts"
label values hh6afdebt assflag

drop hh6aliab hh6afliab

// HO section questions are asked to all household respondents on behalf of 
// the whole household. Even though multiple respondents can complete
// the household section the imputations for these questions are only based
// one of those respondents answers and all members of the household
// are given the same value. Given this couple-level
// variables can only be derived for couples without any additional household
// members. Household-level variables are derived for all respondents.

*generate couple-level value of other real estate variables
generate double h6aores = ores if multihouse == 0
label variable h6aores "h6aores:w6 asset value ores (cpl level)"

imputation_flag ores_f if multihouse == 0, gen(h6afores) by(hh`wv'hhid)
replace h6afores = -1 if multihouse == 1
label variable h6afores "h6afores:w6 asset impu flag ores (cpl level)"
label values h6afores assflag

*generate household-level value of other real estate variables
generate double hh6aores = ores
label variable hh6aores "hh6aores:w6 asset value ores (hh level)"

imputation_flag ores_f, gen(hh6afores) by(hh`wv'hhid)
label variable hh6afores "hh6afores:w6 asset impu flag ores (hh level)"
label values hh6afores assflag

*create more complete imputation variable for value of primary residence
gen home_hf = home_f
replace home_hf = 2 if inlist(otrf,3,4,5) & otrf_f == 3
replace home_hf = 8 if otrf_f == 14

*generate couple-level value of primary residence variables
generate double h6ahome = home if multihouse == 0
label variable h6ahome "h6ahome:w6 asset value home (cpl level)"

imputation_flag home_hf if multihouse == 0, gen(h6afhome) by(hh`wv'hhid)
replace h6afhome = -1 if multihouse == 1
label variable h6afhome "h6afhome:w6 asset impu flag home (cpl level)"
label values h6afhome assflag

*generate household-level value of primary residence variables
generate double hh6ahome = home
label variable hh6ahome "hh6ahome:w6 asset value home (hh level)"

imputation_flag home_hf, gen(hh6afhome) by(hh`wv'hhid)
label variable hh6afhome "hh6afhome:w6 asset impu flag home (hh level)"
label values hh6afhome assflag
drop home_hf

*create more complete imputation variable for value of mortgage
gen mort_hf = mort_f
replace mort_hf = 2 if inlist(otrf,3,4,5) & otrf_f == 3
replace mort_hf = 2 if perho == 0 & perho_f == 3

*generate couple-level value of mortgage variables
generate double h6amort = mort if multihouse == 0
label variable h6amort "h6amort:w6 asset value mort (cpl level)"

imputation_flag mort_hf if multihouse == 0, gen(h6afmort) by(hh`wv'hhid)
replace h6afmort = -1 if multihouse == 1
label variable h6afmort "h6afmort:w6 asset impu flag mort (cpl level)"
label values h6afmort assflag

*generate household-level value of mortage variables
generate double hh6amort = mort
label variable hh6amort "hh6amort:w6 asset value mort (hh level)"

imputation_flag mort_hf, gen(hh6afmort) by(hh`wv'hhid)
label variable hh6afmort "hh6afmort:w6 asset impu flag mort (hh level)"
label values hh6afmort assflag
drop mort_hf

***home ownership***
*wave 6 couple-level home ownership
gen h`wv'ahown = .
replace h`wv'ahown = .m if ho002_ == . & inw`wv' == 1
replace h`wv'ahown = .d if ho002_ == -1
replace h`wv'ahown = .r if ho002_ == -2
replace h`wv'ahown = h`pre_wv'ahown if inrange(h`pre_wv'ahown,0,1) & ho044_ == 5
replace h`wv'ahown = 0 if inlist(ho002_,2,3,4,5,6)
replace h`wv'ahown = 1 if ho002_ == 1
bysort h`wv'coupid: replace h`wv'ahown = h`wv'ahown[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'ahown = h`wv'ahown[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse
replace h`wv'ahown = .m if h`wv'ahown==. & inw`wv'==1
label var h`wv'ahown "h`wv'ahown:w`wv' whether own home"
label values h`wv'ahown homeowner

***value of other real estate
*wave 6 couple-level value of other real estate
generate double h6arles  = h6aores
label variable h6arles "h6arles:w6 Assets:Other Real estate"

*wave 6 couple-level flag for value of other real estate
generate byte h6afrles = h6afores
label variable h6afrles "h6afrles:w6 Asst Flag:Other Real estate"
label values h6afrles assflag

drop h6aores h6afores  

*wave 6 household-level value of other real estate
generate double hh6arles  = hh6aores
label variable hh6arles "hh6arles:w6 Assets:Other Real estate"

*wave 6 household-level flag for value of other real estate
generate byte hh6afrles = hh6afores
label variable hh6afrles "hh6afrles:w6 Asst Flag:Other Real estate"
label values hh6afrles assflag

drop hh6aores hh6afores  

***value of primary residence
*wave 6 couple-level value of primary residence
generate double h6ahous  = h6ahome
label variable h6ahous "h6ahous:w6 Assets:Primary Residence"

*wave 6 couple-level flag for value of primary residence
generate byte h6afhous = h6afhome
label variable h6afhous "h6afhous:w6 Asst Flag:Primary Residence"
label values h6afhous assflag

*wave 6 household-level value of primary residence
generate double hh6ahous  = hh6ahome
label variable hh6ahous "hh6ahous:w6 Assets:Primary Residence"

*wave 6 household-level flag for value of primary residence
generate byte hh6afhous = hh6afhome
label variable hh6afhous "hh6afhous:w6 Asst Flag:Primary Residence"
label values hh6afhous assflag

drop h6ahome h6afhome hh6ahome hh6afhome

***value of mortage
*wave 6 couple-level value of mortgage
label variable h6amort "h6amort:w6 Assets:Mortgage"

*wave 6 couple-level flag for value of mortgage
label variable h6afmort "h6afmort:w6 Asst Flag:Mortgage"

*wave 6 household-level value of mortgage
label variable hh6amort "hh6amort:w6 Assets:Mortgage"

*wave 6 household-level flag for value of mortgage
label variable hh6afmort "hh6afmort:w6 Asst Flag:Mortgage"

// hmln : not in SHARE

drop cplxhh multihouse

//--------------------------------------------------------------------
// Summary variables
//--------------------------------------------------------------------

generate double h`wv'atoth = h`wv'ahous - h`wv'amort
label variable h`wv'atoth "h`wv'atoth:w`wv' Net Value of House /prim res"

h_imputation_flag h`wv'afhous h`wv'afmort, gen(h`wv'aftoth)
label variable h`wv'aftoth "h`wv'aftoth:w`wv' Asst Flag:Net Value of House /prim res"
label values h`wv'aftoth assflag

generate double hh`wv'atoth = hh`wv'ahous - hh`wv'amort
label variable hh`wv'atoth  "hh`wv'atoth:w`wv' Net Value of House /prim res"

h_imputation_flag hh`wv'afhous hh`wv'afmort, gen(hh`wv'aftoth)
label variable hh`wv'aftoth "hh`wv'aftoth:w`wv' Asst Flag:Net Value of House /prim res"
label values hh`wv'aftoth assflag

// houb : not in SHARE
// mrtb : not in SHARE
// nethb : not in SHARE

// totf : not possible with imputations as ira wealth is tied to other long-term savings

generate double h`wv'atotb = h`wv'ahous + h`wv'arles + h`wv'atran + h`wv'absns + h`wv'airothr ///
    + h`wv'abdstk + h`wv'achck - h`wv'adebt - h`wv'amort
label variable h`wv'atotb "h`wv'atotb:w`wv' total of all assets inc. 2nd hm"

h_imputation_flag h`wv'afhous h`wv'afrles h`wv'aftran h`wv'afbsns h`wv'afirothr h`wv'afbdstk ///
                  h`wv'afchck h`wv'afdebt h`wv'afmort, gen(h`wv'aftotb)
label variable h`wv'aftotb "h`wv'aftotb:w`wv' Asst Flag:total of all assets inc. 2nd hm"
label values h`wv'aftotb assflag

generate double hh`wv'atotb = hh`wv'ahous + hh`wv'arles + hh`wv'atran + hh`wv'absns + hh`wv'airothr ///
    + hh`wv'abdstk + hh`wv'achck - hh`wv'adebt - hh`wv'amort
label variable hh`wv'atotb "hh`wv'atotb:w`wv' total of all assets inc. 2nd hm"

h_imputation_flag hh`wv'afhous hh`wv'afrles hh`wv'aftran hh`wv'afbsns hh`wv'afirothr hh`wv'afbdstk ///
                  hh`wv'afchck hh`wv'afdebt hh`wv'afmort, gen(hh`wv'aftotb)
label variable hh`wv'aftotb "hh`wv'aftotb:w`wv' Asst Flag:total of all assets inc. 2nd hm"
label values hh`wv'aftotb assflag

// totb : not relevant for SHARE
// totw : not possible with imputations as ira wealth is tied to other long-term savings

generate double h`wv'atotn = h`wv'arles + h`wv'atran + h`wv'absns + h`wv'airothr + h`wv'abdstk + ///
                             h`wv'achck - h`wv'adebt
label variable h`wv'atotn "h`wv'atotn:w`wv' Total Non-Housing Assets"

h_imputation_flag h`wv'afrles h`wv'aftran h`wv'afbsns h`wv'afirothr h`wv'afbdstk ///
                  h`wv'afchck h`wv'afdebt, gen(h`wv'aftotn)
label variable h`wv'aftotn "h`wv'aftotn:w`wv' Asst Flag:Total Non-Housing Assets"
label values h`wv'aftotn assflag

generate double hh`wv'atotn = hh`wv'arles + hh`wv'atran + hh`wv'absns + hh`wv'airothr + hh`wv'abdstk + ///
                              hh`wv'achck - hh`wv'adebt
label variable hh`wv'atotn "hh`wv'atotn:w`wv' Total Non-Housing Assets"

h_imputation_flag hh`wv'afrles hh`wv'aftran hh`wv'afbsns hh`wv'afirothr hh`wv'afbdstk ///
                  hh`wv'afchck hh`wv'afdebt, gen(hh`wv'aftotn)
label variable hh`wv'aftotn "hh`wv'aftotn:w`wv' Asst Flag:Total Non-Housing Assets"
label values hh`wv'aftotn assflag


***drop wave 6 imputation file raw variables***
drop `asset_w6_imput'

***drop wave 6 household raw variables***
drop `asset_w6_ho'

******************************************************************************************

***Income imputation flag
label define incimpflag ///
   -3 "-3.institutional interview" ///
   -2 "-2.non-responding spouse" ///
   -1 "-1.multiple econ units in hh" ///
    0 "0.No imputation"  ///
    1 "1.Imputation" 

***Income flag
label define incflag ///
  -3 "-3.institutional interview" ///
  -2 "-2.non-responding spouse" ///
  -1 "-1.multiple econ units in hh" ///
   0 "0.No imputation" ///
   1 "1.continuous value" ///
   2 "2.complete bracket"       ///
   3 "3.incomplete bracket"     ///
   5 "5.no value/bracket"        ///
   6 "6.no income"           ///
   7 "7.dk if income"     ///
   9 "9.non-responding spouse"

 
******************************************************************************************

	

*set wave number
local wv = 1

***merge with wave 1 imputation data***
local income_w1_imput yrent ybabsmf   ///
                      yrent_f ybabsmf_f ///
                      ypen1 ypen2 ypen3 ypen4 ypen5 ypen6 yreg1 yreg2 ///
                      ypen1_f ypen2_f ypen3_f ypen4_f ypen5_f ypen6_f yreg1_f yreg2_f ///
                      yind ydip yind_f ydip_f  ///
                      ores_f ///
                      yaohm yaohm_f thinc thinc_f p_nrp
merge 1:1 mergeid using "$wave_1_imputations_", keepusing(`income_w1_imput') nogen

***merge with wave 1 consumption data***
local income_w1_co co002e co003e co004e co005e
merge 1:1 mergeid using "$wave_1_co", keepusing(`income_w1_co') nogen


***create indicator of multiple couples in household
egen cplxhh = tag(hhid1 h1coupid)
egen multihouse = total(cplxhh) if inw1==1, by(hhid1)
recode multihouse (1=0) (2/10 = 1)

***Earnings*** 
// earnings from employment are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Earnings*** 
// earnings from employment are imputed at respondent level
*wave 1 respondent earnings from employment
gen r`wv'iearn = ydip
label variable r`wv'iearn  "r`wv'iearn:w`wv' Income: R Earnings from employment (before-tax)"

*wave 1 respondent flag for earnings from employment
imputation_flag ydip_f, gen(r`wv'ifearn)
label variable r`wv'ifearn "r`wv'ifearn:w`wv' IncFlag: R Earnings from employment (before-tax)"
label values r`wv'ifearn incflag

*wave 1 spouse earnings from employment
gen s`wv'iearn = .
spouse r`wv'iearn, result(s`wv'iearn) wave(`wv')
label variable s`wv'iearn  "s`wv'iearn:w`wv' Income: S Earnings from employment (before-tax)"

*wave 1 spouse flag for earnings from employment
gen s`wv'ifearn = . 
spouse r`wv'ifearn, result(s`wv'ifearn) wave(`wv')
label variable s`wv'ifearn "s`wv'ifearn:w`wv' IncFlag: S Earnings from employment (before-tax)"
label values s`wv'ifearn incflag


***Capital Income***
// earnings from self-employment are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

// rental income is asked of the household respondent on behalf of 
// the whole household. The imputations for these questions are based
// the household respondent's answers and all members of the household
// are given the same value. Given this couple-level
// values can only be derived for couples without any additional household
// members.

// income from bank account and income from bonds, stocks, and mutal 
// funds are asked of the financial respondent on behalf of 
// the respondent and the spouse, if any spouse. The respondent and spouse
// can also choose to answer questions about their finanes separetly in which
// case both the respondent and the spouse answer asset questions. These values
// are imputed at the individual level and at the household level.
// Individual level imputations are available for all financial respondents. 
// All non financial respodents are assigned 0 values for the
// indvidual level asset imputation variables. 


**self-employment income
*wave 1 respondent earnings from self-employment
gen r`wv'isemp = yind
label variable r`wv'isemp "r`wv'isemp:w`wv' Income: R Earnings from self-employment (before-tax)"

*wave 1 respondent flag for earnings from self-employment
imputation_flag yind_f, gen(r`wv'ifsemp)
label variable r`wv'ifsemp "r`wv'ifsemp:w`wv' IncFlag: R Earnings from self-employment (before-tax)"
label values r`wv'ifsemp incflag

*wave 1 spouse earnings from self-employment
gen s`wv'isemp = .
spouse r`wv'isemp, result(s`wv'isemp) wave(`wv')
label variable s`wv'isemp "s`wv'isemp:w`wv' Income: S Earnings from self-employment (before-tax)"

*wave 1 spouse flag for earnings from self-employment
gen s`wv'ifsemp = . 
spouse r`wv'ifsemp, result(s`wv'ifsemp) wave(`wv')
label variable s`wv'ifsemp "s`wv'ifsemp:w`wv' IncFlag: S Earnings from self-employment (before-tax)"
label values s`wv'ifsemp incflag

*wave 1 couple-level earnings from self-employment
egen h`wv'isemp = total(yind) if p_nrp == 0, by(h`wv'coupid)

imputation_flag yind_f, gen(h`wv'ifsemp) by(h`wv'coupid)
replace h`wv'ifsemp = -2 if p_nrp == 1

*wave 1 household-level earnings from self-employment
egen hh`wv'isemp = total(yind) if p_nrp == 0, by(hh`wv'hhid)

imputation_flag yind_f, gen(hh`wv'ifsemp) by(hh`wv'hhid)
replace hh`wv'ifsemp = -2 if p_nrp == 1

**rental income***
*create more complete imputation flag for rental income
gen yrent_hf = yrent_f
replace yrent_hf = 2 if ores_f == 2
replace yrent_hf = 8 if ores_f == 8

*wave 1 couple-level rental income
gen h`wv'irent = yrent if multihouse == 0
label variable h`wv'irent "h`wv'irent:w`wv' Income: R+S Rental income (before-tax)"

imputation_flag yrent_hf, gen(h`wv'ifrent) by(hh`wv'hhid)
replace h`wv'ifrent = -1 if multihouse == 1
label variable h`wv'ifrent "h`wv'ifrent:w`wv' Impflag: R+S Rental income (before-tax)"
label values h`wv'ifrent incflag

*wave 1 household-level rental income
gen hh`wv'irent = yrent
label variable hh`wv'irent "hh`wv'irent:w`wv' Income: HHold Rental income (before-tax)"

imputation_flag yrent_hf, gen(hh`wv'ifrent) by(hh`wv'hhid)
label variable hh`wv'ifrent "hh`wv'ifrent:w`wv' Income: HHold Rental income (before-tax)"
label values hh`wv'ifrent incflag

drop yrent_hf

**interest income from financial assets
*wave 1 couple-level income from bank account, bonds, stocks, and mutual funds
gen h`wv'itrest = ybabsmf
label variable h`wv'itrest "h`wv'itrest:w`wv' Income: R+S Interest income from financial assets (before-tax)"

imputation_flag ybabsmf_f, gen(h`wv'iftrest) by(h`wv'coupid)
label variable h`wv'iftrest "h`wv'iftrest:w`wv' Impflag: R+S Interest income from financial assets (before-tax)"
label values h`wv'iftrest incflag

*wave 1 household-level income from bank account, bonds, stocks, and mutal funds
egen hh`wv'itrest = total(ybabsmf * (cplxhh==1)), by(hh`wv'hhid) m
label variable hh`wv'itrest "hh`wv'itrest:w`wv' Income: HHold Interest income from financial assets (before-tax)"

imputation_flag ybabsmf_f, gen(hh`wv'iftrest) by(hh`wv'hhid)
label variable hh`wv'iftrest "hh`wv'iftrest:w`wv' Impflag: HHold Interest income from financial assets (before-tax)"
label values hh`wv'iftrest inflag

*wave 1 couple-level capital income
generate double h`wv'icap  = h`wv'isemp + h`wv'irent + h`wv'itrest
label variable h`wv'icap  "h`wv'icap:w`wv' Income: R+S Capital Income (before-tax)"

*wave 1 couple-level flag for capital income
h_imputation_flag h`wv'ifsemp h`wv'ifrent h`wv'iftrest, gen(h`wv'ifcap)
label variable h`wv'ifcap "h`wv'ifcap:w`wv' IncFlag: R+S Capital Inc (before-tax)"
label values h`wv'ifcap incflag

*wave 1 household-level capital income
generate double hh`wv'icap = hh`wv'isemp + hh`wv'irent + hh`wv'itrest
label variable hh`wv'icap  "hh`wv'icap:w`wv' Income: HHold Capital Income (before-tax)"

*wave 1 household-level flag for capital income
h_imputation_flag hh`wv'ifsemp hh`wv'ifrent hh`wv'iftrest, gen(hh`wv'ifcap)
label variable hh`wv'ifcap "hh`wv'ifcap:w`wv' IncFlag: HHold Capital Inc (before-tax)"
label values hh`wv'ifcap incflag

drop h`wv'isemp hh`wv'isemp
drop h`wv'ifsemp hh`wv'ifsemp

***Employer/Private Pension and Annuity***
// incomes from pensions are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

// incomes from other regular payments are asked in the employment 
// and pensions module to all respondents. The value is imputed 
// at the respondent level

***Employer/Private Pension and Annuity
*wave 1 respondent income from private and occupational pensions
gen r`wv'ipena = ypen2 + yreg1
label variable r`wv'ipena "r`wv'ipena:w`wv' Income: R Pension + Annuity (before-tax)"

*wave 1 respondent income flag from private and occupational pensions
imputation_flag ypen2_f yreg1_f, gen(r`wv'ifpena)
label variable r`wv'ifpena "r`wv'ifpena:w`wv' ImpFlag: R Pension + Annuity (before-tax)"
label values r`wv'ifpena incflag

*wave 1 spouse income from private and occupational pensions
gen s`wv'ipena = .
spouse r`wv'ipena, result(s`wv'ipena) wave(`wv')
label variable s`wv'ipena "s`wv'ipena:w`wv' Income: S Pension + Annuity (before-tax)"

*wave 1 spouse flag for income from private and occupational pensions
gen s`wv'ifpena = .
spouse r`wv'ifpena, result(s`wv'ifpena) wave(`wv')
label variable s`wv'ifpena "s`wv'ifpena:w`wv' ImpFlag: S Pension + Annuity (before-tax)"
label values s`wv'ifpena incflag

***Public Pensions***
// incomes from pensions are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Public Disability Pensions
*wave 1 respondent income from public disability pensions
gen r`wv'issdi = ypen3
label variable r`wv'issdi "r`wv'issdi:w`wv' Income: R Public Disability Pensions (before-tax)"

*wave 1 respondent flag for income from public disability pensions
imputation_flag ypen3_f, gen(r`wv'ifssdi)
label variable r`wv'ifssdi "r`wv'ifssdi:w`wv' IncFlag: R Public Disability Pensions (before-tax)"
label values r`wv'ifssdi incflag

*wave 1 spouse income from public disability pensions
gen s`wv'issdi = .
spouse r`wv'issdi, result(s`wv'issdi) wave(`wv')
label variable s`wv'issdi "s`wv'issdi:w`wv' Income: S Public Disability Pensions (before-tax)"

*wave 1 spouse flag for income from public disability pensions
gen s`wv'ifssdi = .
spouse r`wv'ifssdi, result(s`wv'ifssdi) wave(`wv')
label variable s`wv'ifssdi "s`wv'ifssdi:w`wv' IncFlag: S Public Disability Pensions (before-tax)"
label values s`wv'ifssdi incflag

***Public Old Age Pensions
*wave 1 respondent income from public old age + early retirement pensions
gen r`wv'isret = ypen1
label variable r`wv'isret "r`wv'isret:w`wv' Income: R Public Old Age + Early Retirement Pensions (before-tax)"

*wave 1 respondent flag for income from  public old age + early retirement pensions
imputation_flag ypen1_f, gen(r`wv'ifsret)
label variable r`wv'ifsret "r`wv'ifsret:w`wv' IncFlag: R Public Old Age + Early Retirement Pensions (before-tax)"
label values r`wv'ifsret incflag

*wave 1 spouse income from public old age + early retirement pensions
gen s`wv'isret = .
spouse r`wv'isret, result(s`wv'isret) wave(`wv')
label variable s`wv'isret "s`wv'isret:w`wv' Income: S Public Old Age + Early Retirement Pensions (before-tax)"

*wave 1 spouse flag for income from public old age + early retirement pensions
gen s`wv'ifsret = .
spouse r`wv'ifsret, result(s`wv'ifsret) wave(`wv')
label variable s`wv'ifsret "s`wv'ifsret:w`wv' IncFlag: S Public Old Age + Early Retirement Pensions (before-tax)"
label values s`wv'ifssdi incflag

***Total Public Pensions
*wave 1 respondent income from total public pensions
gen r`wv'ipubpen = r`wv'issdi + r`wv'isret
label variable r`wv'ipubpen "r`wv'ipubpen:w`wv' Income: R Public Pensions (before-tax)"

*wave 1 respondent flag for income from total public pensions
h_imputation_flag r`wv'ifssdi r`wv'ifsret, gen(r`wv'ifpubpen)
label variable r`wv'ifpubpen "r`wv'ifpubpen:w`wv' Impflag: R Public Pensions (before-tax)"
label values r`wv'ifpubpen incflag

*wave 1 spouse income from total public pensions
gen s`wv'ipubpen = .
spouse r`wv'ipubpen, result(s`wv'ipubpen) wave(`wv')
label variable s`wv'ipubpen "s`wv'ipubpen:w`wv' Income: S Public Pensions (before-tax)"

*wave 1 spouse flag for income from total public pensions
gen s`wv'ifpubpen = .
spouse r`wv'ifpubpen, result(s`wv'ifpubpen) wave(`wv')
label variable s`wv'ifpubpen "s`wv'ifpubpen:w`wv' IncFlag: S Public Pensions (before-tax)"
label values s`wv'ifpubpen incflag

***Other Government Transfers***
// incomes from other goverment transfers are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Other Government Transfers
*wave 1 respondent income from Other Government Transfers
gen r`wv'igxfr = ypen4 + ypen5 + ypen6
label variable r`wv'igxfr "r`wv'igxfr:w`wv' Income: R Other Government Transfers (before-tax)"

*wave 1 respondent flag for income from Other Government Transfers
imputation_flag ypen4_f ypen5_f ypen6_f, gen(r`wv'ifgxfr)
label variable r`wv'ifgxfr "r`wv'ifgxfr:w`wv' IncFlag: R Other Government Transfers (before-tax)"
label values r`wv'ifgxfr incflag

*wave 1 spouse income from Other Government Transfers
gen s`wv'igxfr = .
spouse r`wv'igxfr, result(s`wv'igxfr) wave(`wv')
label variable s`wv'igxfr "s`wv'igxfr:w`wv' Income: S Other Government Transfers (before-tax)"

*wave 1 spouse flag for income from Other Government Transfers
gen s`wv'ifgxfr = .
spouse r`wv'ifgxfr, result(s`wv'ifgxfr) wave(`wv')
label variable s`wv'ifgxfr "s`wv'ifgxfr:w`wv' IncFlag: S Other Government Transfers (before-tax)"
label values s`wv'ifgxfr incflag

***Other Income***
// incomes from other regular payments are asked in the employment 
// and pensions module to all respondents. The value is imputed 
// at the respondent level

*wave 1 respondent income from other income
gen r`wv'iothr =  yreg2
label variable r`wv'iothr "r`wv'iothr:w`wv' Income: R Other Income (before-tax)"

*wave 1 respondent flag for income from other income
imputation_flag yreg2_f, gen(r`wv'ifothr)
label variable r`wv'ifothr "r`wv'ifothr:w`wv' IncFlag: R Other Income (before-tax)"
label values r`wv'ifothr incflag

*wave 1 spouse income from other income
gen s`wv'iothr = .
spouse r`wv'iothr, result(s`wv'iothr) wave(`wv')
label variable s`wv'iothr "s`wv'iothr:w`wv' Income: S Other Income (before-tax)"

*wave 1 spouse flag for income from other income
gen s`wv'ifothr = .
spouse r`wv'ifothr, result(s`wv'ifothr) wave(`wv')
label variable s`wv'ifothr "s`wv'ifothr:w`wv' IncFlag: S Other Income (before-tax)"
label values s`wv'ifothr incflag

***Income from other household members***
// income from other household members is imputed at full-household level

***Income from other household members
*wave 1 household-level income from other household members
gen hh`wv'iothhh = yaohm
label variable hh`wv'iothhh "hh`wv'iothhh:w`wv' Income: HHold Other Household Members' Inc (before-tax)"

imputation_flag yaohm_f, gen(hh`wv'ifothhh) by(hh`wv'hhid)
label variable hh`wv'ifothhh "hh`wv'ifothhh:w`wv' Impflag: HHold Other Household Members' Inc (before-tax)"
label values hh`wv'ifothhh incflag

***Total Income***

*couple-level earnings from employment
egen h`wv'iearn = total(r`wv'iearn) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'ifearn, gen(h`wv'ifearn) by(h`wv'coupid)
replace h`wv'ifearn = -2 if p_nrp == 1

*couple-level income from private pensions
egen h`wv'ipena = total(r`wv'ipena) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'ifpena, gen(h`wv'ifpena) by(h`wv'coupid)
replace h`wv'ifpena = -2 if p_nrp == 1

*couple-level income from public pensions
egen h`wv'ipubpen = total(r`wv'ipubpen) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'ifpubpen, gen(h`wv'ifpubpen) by(h`wv'coupid)
replace h`wv'ifpubpen = -2 if p_nrp == 1

*couple-level income from goverment transfers
egen h`wv'igxfr = total(r`wv'igxfr) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'ifgxfr, gen(h`wv'ifgxfr) by(h`wv'coupid)
replace h`wv'ifgxfr = -2 if p_nrp == 1

*couple-level income from other income
egen h`wv'iothr = total(r`wv'iothr) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'ifothr, gen(h`wv'ifothr) by(h`wv'coupid)
replace h`wv'ifothr = -2 if p_nrp == 1

*household-level earnings from employment
egen hh`wv'iearn = total(r`wv'iearn) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'ifearn, gen(hh`wv'ifearn) by(hh`wv'hhid)
replace hh`wv'ifearn = -2 if p_nrp == 1

*household-level income from private pensions
egen hh`wv'ipena = total(r`wv'ipena) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'ifpena, gen(hh`wv'ifpena) by(hh`wv'hhid)
replace hh`wv'ifpena = -2 if p_nrp == 1

*household-level income from public pensions
egen hh`wv'ipubpen = total(r`wv'ipubpen) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'ifpubpen, gen(hh`wv'ifpubpen) by(hh`wv'hhid)
replace hh`wv'ifpubpen = -2 if p_nrp == 1

*household-level income from government transfers
egen hh`wv'igxfr = total(r`wv'igxfr) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'ifgxfr, gen(hh`wv'ifgxfr) by(hh`wv'hhid)
replace hh`wv'ifgxfr = -2 if p_nrp == 1

*household-level income from other income
egen hh`wv'iothr = total(r`wv'iothr) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'ifothr, gen(hh`wv'ifothr) by(hh`wv'hhid)
replace hh`wv'ifothr = -2 if p_nrp == 1

*household-level total income
gen hh`wv'itotinc = thinc

imputation_flag thinc_f, gen(hh`wv'iftotinc) by(hh`wv'hhid)

*wave 1 couple-level total income
generate double h`wv'itot = h`wv'iearn + h`wv'icap + h`wv'ipena + h`wv'ipubpen + h`wv'igxfr + h`wv'iothr
label variable h`wv'itot "h`wv'itot:w`wv' Incm: R+S Total Income (before-tax)"

*wave 1 couple-level flag for total income
h_imputation_flag h`wv'ifearn h`wv'ifcap h`wv'ifpena h`wv'ifpubpen h`wv'ifgxfr h`wv'ifothr, gen(h`wv'iftot)
label variable h`wv'iftot "h`wv'iftot:w`wv' IncFlag: R+S Total Inc (before-tax)"
label values h`wv'iftot incflag

*wave 1 household-level total income
generate double hh`wv'itot = hh`wv'itotinc
label variable hh`wv'itot "hh`wv'itot:w`wv' Incm: HHold Total Income (before-tax)"

*wave 1 household-level flag for total income
h_imputation_flag hh`wv'ifearn hh`wv'ifcap hh`wv'ifpena hh`wv'ifpubpen hh`wv'ifgxfr hh`wv'ifothr hh`wv'ifothhh, gen(hh`wv'iftot)
replace hh`wv'iftot = -2 if hh`wv'iftotinc == 9
label variable hh`wv'iftot "hh`wv'iftot:w`wv' IncFlag: HHold Total Income (before-tax)"
label values hh`wv'iftot incflag

drop h`wv'iearn  h`wv'ipena  h`wv'ipubpen  h`wv'iothr h`wv'igxfr ///
		 h`wv'ifearn h`wv'ifpena h`wv'ifpubpen h`wv'ifothr h`wv'ifgxfr ///
		 hh`wv'iearn  hh`wv'ipena  hh`wv'ipubpen  hh`wv'iothr hh`wv'igxfr hh`wv'itotinc  ///
		 hh`wv'ifearn hh`wv'ifpena hh`wv'ifpubpen hh`wv'ifothr hh`wv'ifgxfr hh`wv'iftotinc
		 
drop cplxhh multihouse

***Consumption: food at home***
*wave 1 household consumption food at home
gen hh`wv'cfoodi1m = .
replace hh`wv'cfoodi1m = .m if co002e == . & inw`wv' == 1
replace hh`wv'cfoodi1m = .r if co002e == -9999992
replace hh`wv'cfoodi1m = .d if co002e == -9999991
replace hh`wv'cfoodi1m = co002e if inrange(co002e,0,1000000)
label variable hh`wv'cfoodi1m "hh`wv'cfoodi1m:w`wv' hhold monthly food consumption in house"

bysort h`wv'coupid: replace hh`wv'cfoodi1m = hh`wv'cfoodi1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfoodi1m = hh`wv'cfoodi1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

***Consumption: food outside home***
*wave 1 household consumption food outside home
gen hh`wv'cfoodo1m = .
replace hh`wv'cfoodo1m = .m if co003e == . & inw`wv' == 1
replace hh`wv'cfoodo1m = .r if co003e == -9999992
replace hh`wv'cfoodo1m = .d if co003e == -9999991
replace hh`wv'cfoodo1m = co003e if inrange(co003e,0,50000)
label variable hh`wv'cfoodo1m "hh`wv'cfoodo1m:w`wv' hhold monthly food consumption outside house"

bysort h`wv'coupid: replace hh`wv'cfoodo1m = hh`wv'cfoodo1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfoodo1m = hh`wv'cfoodo1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

***Consumption: total food***
*wave 1 household consumption total food
gen hh`wv'cfood1m = .
replace hh`wv'cfood1m = .m if (co002e == . | co003e == .)  & inw`wv' == 1
replace hh`wv'cfood1m = .r if co002e == -9999992 | co003e == -9999992
replace hh`wv'cfood1m = .d if co002e == -9999991 | co003e == -9999991
replace hh`wv'cfood1m = co002e + co003e if inrange(co002e,0,1000000) & inrange(co003e,0,50000)
label variable hh`wv'cfood1m "hh`wv'cfood1m:w`wv' hhold total monthly food consumption"

bysort h`wv'coupid: replace hh`wv'cfood1m = hh`wv'cfood1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfood1m = hh`wv'cfood1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

****Consumption: telephone***
**wave 1 household consumption telephone
*gen hh`wv'cphone1m = .
*replace hh`wv'cphone1m = .m if co004e == .
*replace hh`wv'cphone1m = .r if co004e == -9999992
*replace hh`wv'cphone1m = .d if co004e == -9999991
*replace hh`wv'cphone1m = .m if co004e == 900000000000
*replace hh`wv'cphone1m = co004e if inrange(co004e,0,15000000)
*label variable hh`wv'cphone1m "hh`wv'cphone1m:w`wv' hhold monthly telephone consumption"
*
*bysort h`wv'coupid: replace hh`wv'cphone1m = hh`wv'cphone1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
*bysort h`wv'coupid: replace hh`wv'cphone1m = hh`wv'cphone1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

***Consumption: total***
*wave 1 household total consumption
gen hh`wv'ctot1m = .
replace hh`wv'ctot1m = .m if co005e == . & inw`wv' == 1
replace hh`wv'ctot1m = .r if co005e == -9999992
replace hh`wv'ctot1m = .d if co005e == -9999991
missing_common co005e, result(hh`wv'ctot1m)
replace hh`wv'ctot1m = co005e if inrange(co005e,0,5000000)
label variable hh`wv'ctot1m "hh`wv'ctot1m:w`wv' hhold monthly total consumption"

bysort h`wv'coupid: replace hh`wv'ctot1m = hh`wv'ctot1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'ctot1m = hh`wv'ctot1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse


***drop wave 1 imputation file raw variables***
drop `income_w1_imput'

***drop wave 1 consumption file raw variables***
drop `income_w1_co'

******************************************************************************************

	

*set wave number
local wv = 2

***merge with wave 2 imputation data***
local income_w2_imput yrent ybabsmf   ///
                      yrent_f ybabsmf_f ///
                      ypen1 ypen2 ypen36 ypen4 ypen5 ///
                      ypen1_f ypen2_f ypen36_f ypen4_f ypen5_f ///
                      ylsp1 ylsp2 ylsp36 ylsp4 ylsp5 ///
                      ylsp1_f ylsp2_f ylsp36_f ylsp4_f ylsp5_f ///
                      yreg1 yreg2 ///
                      yreg1_f yreg2_f ///
                      ylsr1 ylsr2 ///
                      ylsr1_f ylsr2_f ///
                      yind ydip yind_f ydip_f  ///
                      ores_f ///
                      yaohm yaohm_f thinc thinc_f p_nrp 
merge 1:1 mergeid using "$wave_2_imputations_", keepusing(`income_w2_imput') nogen

***merge with wave 2 consumption data***
local income_w2_co co002e co003e co004e co010_ co011e 
merge 1:1 mergeid using "$wave_2_co", keepusing(`income_w2_co') nogen


***create indicator of multiple couples in household
egen cplxhh = tag(hhid2 h2coupid)
egen multihouse = total(cplxhh) if inw2==1, by(hhid2)
recode multihouse (1=0) (2/10 = 1)

***Earnings*** 
// earnings from employment are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Earnings*** 
// earnings from employment are imputed at respondent level
*wave 2 respondent earnings from employment
gen r`wv'itearn = ydip
label variable r`wv'itearn  "r`wv'itearn:w`wv' Income: R Earnings from employment (after-tax)"

*wave 2 respondent flag for earnings from employment
imputation_flag ydip_f, gen(r`wv'itfearn)
label variable r`wv'itfearn "r`wv'itfearn:w`wv' IncFlag: R Earnings from employment (after-tax)"
label values r`wv'itfearn incflag

*wave 2 spouse earnings from employment
gen s`wv'itearn = .
spouse r`wv'itearn, result(s`wv'itearn) wave(`wv')
label variable s`wv'itearn  "s`wv'itearn:w`wv' Income: S Earnings from employment  (after-tax)"

*wave 2 spouse flag for earnings from employment
gen s`wv'itfearn = . 
spouse r`wv'itfearn, result(s`wv'itfearn) wave(`wv')
label variable s`wv'itfearn "s`wv'itfearn:w`wv' IncFlag: S Earnings from employment (after-tax)"
label values s`wv'itfearn incflag


***Capital Income***
// earnings from self-employment are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

// rental income is asked of the household respondent on behalf of 
// the whole household. The imputations for these questions are based
// the household respondent's answers and all members of the household
// are given the same value. Given this couple-level
// values can only be derived for couples without any additional household
// members.

// income from bank account and income from bonds, stocks, and mutal 
// funds are asked of the financial respondent on behalf of 
// the respondent and the spouse, if any spouse. The respondent and spouse
// can also choose to answer questions about their finanes separetly in which
// case both the respondent and the spouse answer asset questions. These values
// are imputed at the individual level and at the household level.
// Individual level imputations are available for all financial respondents. 
// All non financial respodents are assigned 0 values for the
// indvidual level asset imputation variables. 


**self-employment income
*wave 2 respondent earnings from self-employment
gen r`wv'itsemp = yind
label variable r`wv'itsemp "r`wv'itsemp:w`wv' Income: R Earnings from self-employment (after-tax)"

*wave 2 respondent flag for earnings from self-employment
imputation_flag yind_f, gen(r`wv'itfsemp)
label variable r`wv'itfsemp "r`wv'itfsemp:w`wv' IncFlag: R Earnings from self-employment (after-tax)"
label values r`wv'itfsemp incflag

*wave 2 spouse earnings from self-employment
gen s`wv'itsemp = .
spouse r`wv'itsemp, result(s`wv'itsemp) wave(`wv')
label variable s`wv'itsemp "s`wv'itsemp:w`wv' Income: S Earnings from self-employment (after-tax)"

*wave 2 spouse flag for earnings from self-employment
gen s`wv'itfsemp = . 
spouse r`wv'itfsemp, result(s`wv'itfsemp) wave(`wv')
label variable s`wv'itfsemp "s`wv'itfsemp:w`wv' IncFlag: S Earnings from self-employment (after-tax)"
label values s`wv'itfsemp incflag

*wave 2 couple-level earnings from self-employment
egen h`wv'itsemp = total(yind) if p_nrp == 0, by(h`wv'coupid)

imputation_flag yind_f, gen(h`wv'itfsemp) by(h`wv'coupid)
replace h`wv'itfsemp = -2 if p_nrp == 1

*wave 2 household-level earnings from self-employment
egen hh`wv'itsemp = total(yind) if p_nrp == 0, by(hh`wv'hhid)

imputation_flag yind_f, gen(hh`wv'itfsemp) by(hh`wv'hhid)
replace hh`wv'itfsemp = -2 if p_nrp == 1

**rental income***
*create more complete imputation flag for rental income
gen yrent_hf = yrent_f
replace yrent_hf = 2 if ores_f == 2
replace yrent_hf = 8 if ores_f == 8

*wave 2 couple-level rental income
gen h`wv'itrent = yrent if multihouse == 0
label variable h`wv'itrent "h`wv'itrent:w`wv' Income: R+S Rental income (after-tax)"

imputation_flag yrent_hf, gen(h`wv'itfrent) by(hh`wv'hhid)
replace h`wv'itfrent = -1 if multihouse == 1
label variable h`wv'itfrent "h`wv'itfrent:w`wv' Impflag: R+S Rental income (after-tax)"
label values h`wv'itfrent incflag

*wave 2 household-level rental income
gen hh`wv'itrent = yrent
label variable hh`wv'itrent "hh`wv'itrent:w`wv' Income: HHold Rental income (after-tax)"

imputation_flag yrent_hf, gen(hh`wv'itfrent) by(hh`wv'hhid)
label variable hh`wv'itfrent "hh`wv'itfrent:w`wv' Income: HHold Rental income (after-tax)"
label values hh`wv'itfrent incflag

drop yrent_hf

**interest income from financial assets
*wave 2 couple-level income from bank account, bonds, stocks, and mutual funds
gen h`wv'ittrest = ybabsmf
label variable h`wv'ittrest "h`wv'ittrest:w`wv' Income: R+S Interest income from financial assets (after-tax)"

imputation_flag ybabsmf_f, gen(h`wv'itftrest) by(h`wv'coupid)
label variable h`wv'itftrest "h`wv'itftrest:w`wv' Impflag: R+S Interest income from financial assets (after-tax)"
label values h`wv'itftrest incflag

*wave 2 household-level income from bank account, bonds, stocks, and mutal funds
egen hh`wv'ittrest = total(ybabsmf * (cplxhh==1)), by(hh`wv'hhid) m
label variable hh`wv'ittrest "hh`wv'ittrest:w`wv' Income: HHold Interest income from financial assets (after-tax)"

imputation_flag ybabsmf_f, gen(hh`wv'itftrest) by(hh`wv'hhid)
label variable hh`wv'itftrest "hh`wv'itftrest:w`wv' Impflag: HHold Interest income from financial assets (after-tax)"
label values hh`wv'itftrest incflag

*wave 2 couple-level capital income
generate double h`wv'itcap  = h`wv'itsemp + h`wv'itrent + h`wv'ittrest
label variable h`wv'itcap  "h`wv'itcap:w`wv' Income: R+S Capital Income (after-tax)"

*wave 2 couple-level flag for capital income
h_imputation_flag h`wv'itfsemp h`wv'itfrent h`wv'itftrest, gen(h`wv'itfcap)
label variable h`wv'itfcap "h`wv'itfcap:w`wv' IncFlag: R+S Capital Inc (after-tax)"
label values h`wv'itfcap incflag

*wave 2 household-level capital income
generate double hh`wv'itcap = hh`wv'itsemp + hh`wv'itrent + hh`wv'ittrest
label variable hh`wv'itcap  "hh`wv'itcap:w`wv' Income: HHold Capital Income (after-tax)"

*wave 2 household-level flag for capital income
h_imputation_flag hh`wv'itfsemp hh`wv'itfrent hh`wv'itftrest, gen(hh`wv'itfcap)
label variable hh`wv'itfcap "hh`wv'itfcap:w`wv' IncFlag: HHold Capital Inc (after-tax)"
label values hh`wv'itfcap incflag

drop h`wv'itsemp hh`wv'itsemp
drop h`wv'itfsemp hh`wv'itfsemp

***Employer/Private Pension and Annuity***
// incomes from pensions are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

// incomes from other regular payments are asked in the employment 
// and pensions module to all respondents. The value is imputed 
// at the respondent level

***Employer/Private Pension and Annuity
*wave 2 respondent income from private and occupational pensions
gen r`wv'itpena = ypen2 + yreg1 + ylsp2 + ylsr1
label variable r`wv'itpena "r`wv'itpena:w`wv' Income: R Pension + Annuity (after-tax)"

*wave 2 respondent income flag from private and occupational pensions
imputation_flag ypen2_f yreg1_f ylsp2_f ylsr1_f, gen(r`wv'itfpena)
label variable r`wv'itfpena "r`wv'itfpena:w`wv' ImpFlag: R Pension + Annuity (after-tax)"
label values r`wv'itfpena incflag

*wave 2 spouse income from private and occupational pensions
gen s`wv'itpena = .
spouse r`wv'itpena, result(s`wv'itpena) wave(`wv')
label variable s`wv'itpena "s`wv'itpena:w`wv' Income: S Pension + Annuity (after-tax)"

*wave 2 spouse flag for income from private and occupational pensions
gen s`wv'itfpena = .
spouse r`wv'itfpena, result(s`wv'itfpena) wave(`wv')
label variable s`wv'itfpena "s`wv'itfpena:w`wv' ImpFlag: S Pension + Annuity (after-tax)"
label values s`wv'itfpena incflag

***Public Pensions***
// incomes from pensions are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Public Disability Pensions
*wave 2 respondent income from public disability pensions
gen r`wv'itssdi = ypen36 + ylsp36
label variable r`wv'itssdi "r`wv'itssdi:w`wv' Income: R Public Disability Pensions (after-tax)"

*wave 2 respondent flag for income from public disability pensions
imputation_flag ypen36_f ylsp36_f, gen(r`wv'itfssdi)
label variable r`wv'itfssdi "r`wv'itfssdi:w`wv' IncFlag: R Public Disability Pensions (after-tax)"
label values r`wv'itfssdi incflag

*wave 2 spouse income from public disability pensions
gen s`wv'itssdi = .
spouse r`wv'itssdi, result(s`wv'itssdi) wave(`wv')
label variable s`wv'itssdi "s`wv'itssdi:w`wv' Income: S Public Disability Pensions (after-tax)"

*wave 2 spouse flag for income from public disability pensions
gen s`wv'itfssdi = .
spouse r`wv'itfssdi, result(s`wv'itfssdi) wave(`wv')
label variable s`wv'itfssdi "s`wv'itfssdi:w`wv' IncFlag: S Public Disability Pensions (after-tax)"
label values s`wv'itfssdi incflag

***Public Old Age Pensions
*wave 2 respondent income from public old age + early retirement pensions
gen r`wv'itsret = ypen1 + ylsp1
label variable r`wv'itsret "r`wv'itsret:w`wv' Income: R Public Old Age + Early Retirement Pensions (after-tax)"

*wave 2 respondent flag for income from  public old age + early retirement pensions
imputation_flag ypen1_f ylsp1_f, gen(r`wv'itfsret)
label variable r`wv'itfsret "r`wv'itfsret:w`wv' IncFlag: R Public Old Age + Early Retirement Pensions (after-tax)"
label values r`wv'itfsret incflag

*wave 2 spouse income from public old age + early retirement pensions
gen s`wv'itsret = .
spouse r`wv'itsret, result(s`wv'itsret) wave(`wv')
label variable s`wv'itsret "s`wv'itsret:w`wv' Income: S Public Old Age + Early Retirement Pensions (after-tax)"

*wave 2 spouse flag for income from public old age + early retirement pensions
gen s`wv'itfsret = .
spouse r`wv'itfsret, result(s`wv'itfsret) wave(`wv')
label variable s`wv'itfsret "s`wv'itfsret:w`wv' IncFlag: S Public Old Age + Early Retirement Pensions (after-tax)"
label values s`wv'itfssdi incflag

***Total Public Pensions
*wave 2 respondent income from total public pensions
gen r`wv'itpubpen = r`wv'itssdi + r`wv'itsret
label variable r`wv'itpubpen "r`wv'itpubpen:w`wv' Income: R Public Pensions (after-tax)"

*wave 2 respondent flag for income from total public pensions
h_imputation_flag r`wv'itfssdi r`wv'itfsret, gen(r`wv'itfpubpen)
label variable r`wv'itfpubpen "r`wv'itfpubpen:w`wv' Impflag: R Public Pensions (after-tax)"
label values r`wv'itfpubpen incflag

*wave 2 spouse income from total public pensions
gen s`wv'itpubpen = .
spouse r`wv'itpubpen, result(s`wv'itpubpen) wave(`wv')
label variable s`wv'itpubpen "s`wv'itpubpen:w`wv' Income: S Public Pensions (after-tax)"

*wave 2 spouse flag for income from total public pensions
gen s`wv'itfpubpen = .
spouse r`wv'itfpubpen, result(s`wv'itfpubpen) wave(`wv')
label variable s`wv'itfpubpen "s`wv'itfpubpen:w`wv' IncFlag: S Public Pensions (after-tax)"
label values s`wv'itfpubpen incflag

***Other Government Transfers***
// incomes from other goverment transfers are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Other Government Transfers
*wave 2 respondent income from Other Government Transfers
gen r`wv'itgxfr = ypen4 + ypen5 + ylsp4 + ylsp5
label variable r`wv'itgxfr "r`wv'itgxfr:w`wv' Income: R Other Government Transfers (after-tax)"

*wave 2 respondent flag for income from Other Government Transfers
imputation_flag ypen4_f ypen5_f ylsp4_f ylsp5_f, gen(r`wv'itfgxfr)
label variable r`wv'itfgxfr "r`wv'itfgxfr:w`wv' IncFlag: R Other Government Transfers (after-tax)"
label values r`wv'itfgxfr incflag

*wave 2 spouse income from Other Government Transfers
gen s`wv'itgxfr = .
spouse r`wv'itgxfr, result(s`wv'itgxfr) wave(`wv')
label variable s`wv'itgxfr "s`wv'itgxfr:w`wv' Income: S Other Government Transfers (after-tax)"

*wave 2 spouse flag for income from Other Government Transfers
gen s`wv'itfgxfr = .
spouse r`wv'itfgxfr, result(s`wv'itfgxfr) wave(`wv')
label variable s`wv'itfgxfr "s`wv'itfgxfr:w`wv' IncFlag: S Other Government Transfers (after-tax)"
label values s`wv'itfgxfr incflag

***Other Income***
// incomes from other regular payments are asked in the employment 
// and pensions module to all respondents. The value is imputed 
// at the respondent level

*wave 2 respondent income from other income
gen r`wv'itothr =  yreg2 + ylsr2
label variable r`wv'itothr "r`wv'itothr:w`wv' Income: R Other Income (after-tax)"

*wave 2 respondent flag for income from other income
imputation_flag yreg2_f ylsr2_f, gen(r`wv'itfothr)
label variable r`wv'itfothr "r`wv'itfothr:w`wv' IncFlag: R Other Income (after-tax)"
label values r`wv'itfothr incflag

*wave 2 spouse income from other income
gen s`wv'itothr = .
spouse r`wv'itothr, result(s`wv'itothr) wave(`wv')
label variable s`wv'itothr "s`wv'itothr:w`wv' Income: S Other Income (after-tax)"

*wave 2 spouse flag for income from other income
gen s`wv'itfothr = .
spouse r`wv'itfothr, result(s`wv'itfothr) wave(`wv')
label variable s`wv'itfothr "s`wv'itfothr:w`wv' IncFlag: S Other Income (after-tax)"
label values s`wv'itfothr incflag

***Income from other household members***
// income from other household members is imputed at full-household level

***Income from other household members
*wave 2 household-level income from other household members
gen hh`wv'itothhh = yaohm
label variable hh`wv'itothhh "hh`wv'itothhh:w`wv' Income: HHold Other Household Members' Inc (after-tax)"

imputation_flag yaohm_f, gen(hh`wv'itfothhh) by(hh`wv'hhid)
label variable hh`wv'itfothhh "hh`wv'itfothhh:w`wv' Impflag: HHold Other Household Members' Inc (after-tax)"
label values hh`wv'itfothhh incflag

***Total Income***

*couple-level earnings from employment
egen h`wv'itearn = total(r`wv'itearn) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfearn, gen(h`wv'itfearn) by(h`wv'coupid)
replace h`wv'itfearn = -2 if p_nrp == 1

*couple-level income from private pensions
egen h`wv'itpena = total(r`wv'itpena) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfpena, gen(h`wv'itfpena) by(h`wv'coupid)
replace h`wv'itfpena = -2 if p_nrp == 1

*couple-level income from public pensions
egen h`wv'itpubpen = total(r`wv'itpubpen) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfpubpen, gen(h`wv'itfpubpen) by(h`wv'coupid)
replace h`wv'itfpubpen = -2 if p_nrp == 1

*couple-level income from goverment transfers
egen h`wv'itgxfr = total(r`wv'itgxfr) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfgxfr, gen(h`wv'itfgxfr) by(h`wv'coupid)
replace h`wv'itfgxfr = -2 if p_nrp == 1

*couple-level income from other income
egen h`wv'itothr = total(r`wv'itothr) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfothr, gen(h`wv'itfothr) by(h`wv'coupid)
replace h`wv'itfothr = -2 if p_nrp == 1

*household-level earnings from employment
egen hh`wv'itearn = total(r`wv'itearn) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfearn, gen(hh`wv'itfearn) by(hh`wv'hhid)
replace hh`wv'itfearn = -2 if p_nrp == 1

*household-level income from private pensions
egen hh`wv'itpena = total(r`wv'itpena) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfpena, gen(hh`wv'itfpena) by(hh`wv'hhid)
replace hh`wv'itfpena = -2 if p_nrp == 1

*household-level income from public pensions
egen hh`wv'itpubpen = total(r`wv'itpubpen) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfpubpen, gen(hh`wv'itfpubpen) by(hh`wv'hhid)
replace hh`wv'itfpubpen = -2 if p_nrp == 1

*household-level income from government transfers
egen hh`wv'itgxfr = total(r`wv'itgxfr) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfgxfr, gen(hh`wv'itfgxfr) by(hh`wv'hhid)
replace hh`wv'itfgxfr = -2 if p_nrp == 1

*household-level income from other income
egen hh`wv'itothr = total(r`wv'itothr) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfothr, gen(hh`wv'itfothr) by(hh`wv'hhid)
replace hh`wv'itfothr = -2 if p_nrp == 1

*household-level total income
gen hh`wv'ittotinc = thinc

imputation_flag thinc_f, gen(hh`wv'itftotinc) by(hh`wv'hhid)

*wave 2 couple-level total income
generate double h`wv'ittot = h`wv'itearn + h`wv'itcap + h`wv'itpena + h`wv'itpubpen + h`wv'itgxfr + h`wv'itothr
label variable h`wv'ittot "h`wv'ittot:w`wv' Incm: R+S Total Income (after-tax)"

*wave 2 couple-level flag for total income
h_imputation_flag h`wv'itfearn h`wv'itfcap h`wv'itfpena h`wv'itfpubpen h`wv'itfgxfr h`wv'itfothr, gen(h`wv'itftot)
label variable h`wv'itftot "h`wv'itftot:w`wv' IncFlag: R+S Total Inc (after-tax)"
label values h`wv'itftot incflag

*wave 2 household-level total income
generate double hh`wv'ittot = hh`wv'ittotinc
label variable hh`wv'ittot "hh`wv'ittot:w`wv' Incm: HHold Total Income (after-tax)"

*wave 2 household-level flag for total income
h_imputation_flag hh`wv'itfearn hh`wv'itfcap hh`wv'itfpena hh`wv'itfpubpen hh`wv'itfgxfr hh`wv'itfothr hh`wv'itfothhh, gen(hh`wv'itftot)
replace hh`wv'itftot = -2 if hh`wv'itftotinc == 9
label variable hh`wv'itftot "hh`wv'itftot:w`wv' IncFlag: HHold Total Income (after-tax)"
label values hh`wv'itftot incflag

drop h`wv'itearn  h`wv'itpena  h`wv'itpubpen  h`wv'itothr h`wv'itgxfr ///
		 h`wv'itfearn h`wv'itfpena h`wv'itfpubpen h`wv'itfothr h`wv'itfgxfr ///
		 hh`wv'itearn  hh`wv'itpena  hh`wv'itpubpen  hh`wv'itothr hh`wv'itgxfr hh`wv'ittotinc  ///
		 hh`wv'itfearn hh`wv'itfpena hh`wv'itfpubpen hh`wv'itfothr hh`wv'itfgxfr hh`wv'itftotinc

drop cplxhh multihouse	

***Consumption: food at home***
*wave 2 household consumption food at home
gen foodh = .
replace foodh = 0 if co010_ == 5
replace foodh = co011e if inrange(co011e,0,100000) & co010_ == 1
gen foodi = .
replace foodi = co002e if inrange(co002e,0,100000)
gen hh`wv'cfoodi1m = .
replace hh`wv'cfoodi1m = .m if (co002e == . | co011e == . | co010_ == .) & inw`wv' == 1
replace hh`wv'cfoodi1m = .r if co002e == -9999992 | co010_ == -9999992 | co011e == -9999992
replace hh`wv'cfoodi1m = .d if co002e == -9999991 | co010_ == -9999991 | co011e == -9999991
replace hh`wv'cfoodi1m = foodh + foodi if inrange(foodh,0,1000000) & inrange(foodi,0,1000000)
label variable hh`wv'cfoodi1m "hh`wv'cfoodi1m:w`wv' hhold monthly food consumption in house"

bysort h`wv'coupid: replace hh`wv'cfoodi1m = hh`wv'cfoodi1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfoodi1m = hh`wv'cfoodi1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

drop foodh foodi

***Consumption: food outside home***
*wave 2 household consumption food outside home
gen hh`wv'cfoodo1m = .
replace hh`wv'cfoodo1m = .m if co003e == . & inw`wv' == 1
replace hh`wv'cfoodo1m = .r if co003e == -9999992
replace hh`wv'cfoodo1m = .d if co003e == -9999991
replace hh`wv'cfoodo1m = co003e if inrange(co003e,0,50000)
label variable hh`wv'cfoodo1m "hh`wv'cfoodo1m:w`wv' hhold monthly food consumption outside house"

bysort h`wv'coupid: replace hh`wv'cfoodo1m = hh`wv'cfoodo1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfoodo1m = hh`wv'cfoodo1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

***Consumption: total food***
*wave 1 household consumption total food
gen hh`wv'cfood1m = .
replace hh`wv'cfood1m = .m if (co002e == . | co003e == . | co011e == . | co010_ == .) & inw`wv' == 1
replace hh`wv'cfood1m = .r if co002e == -9999992 | co003e == -9999992 | co011e == -9999992 | co010_ == -9999992
replace hh`wv'cfood1m = .d if co002e == -9999991 | co003e == -9999991 | co011e == -9999991 | co010_ == -9999991
replace hh`wv'cfood1m = hh`wv'cfoodi1m + hh`wv'cfoodo1m if inrange(hh`wv'cfoodi1m,0,2000000) & inrange(hh`wv'cfoodo1m,0,50000)
label variable hh`wv'cfood1m "hh`wv'cfood1m:w`wv' hhold total monthly food consumption"

bysort h`wv'coupid: replace hh`wv'cfood1m = hh`wv'cfood1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfood1m = hh`wv'cfood1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

****Consumption: telephone***
**wave 2 household consumption telephone
*gen hh`wv'cphone1m = .
*replace hh`wv'cphone1m = .m if co004e == .
*replace hh`wv'cphone1m = .r if co004e == -9999992
*replace hh`wv'cphone1m = .d if co004e == -9999991
*replace hh`wv'cphone1m = co004e if inrange(co004e,0,15000000)
*label variable hh`wv'cphone1m "hh`wv'cphone1m:w`wv' hhold monthly telephone consumption"
*
*bysort h`wv'coupid: replace hh`wv'cphone1m = hh`wv'cphone1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
*bysort h`wv'coupid: replace hh`wv'cphone1m = hh`wv'cphone1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse


***drop ELSA wave 2 financial derived file raw variables***
drop `income_w2_imput'

***drop wave 2 consumption file raw variables***
drop `income_w2_co'

******************************************************************************************

*set wave number
local wv = 4

***merge with wave 4 imputation data***
local income_w4_imput ysrent ybabsmf  ///
                      ysrent_f ybabsmf_f ///
                      ypen1 ypen2 ypen36 ypen4 ypen5 ///
                      ypen1_f ypen2_f ypen36_f ypen4_f ypen5_f ///
                      ylsp1 ylsp2 ylsp36 ylsp4 ylsp5 ///
                      ylsp1_f ylsp2_f ylsp36_f ylsp4_f ylsp5_f ///
                      yreg1 yreg2 ///
                      yreg1_f yreg2_f ///
                      ylsr1 ylsr2 ///
                      ylsr1_f ylsr2_f ///
                      yind ydip yind_f ydip_f  ///
                      ores_f ///
                      bacc bacc_f bsmf bsmf_f ///
                      yaohm yaohm_f thinc thinc_f p_nrp 
merge 1:1 mergeid using "$wave_4_imputations_", keepusing(`income_w4_imput') nogen

***merge with wave 4 consumption data***
local income_w4_co co002e co003e co010_ co011e 
merge 1:1 mergeid using "$wave_4_co", keepusing(`income_w4_co') nogen


***create indicator of multiple couples in household
egen cplxhh = tag(hhid4 h4coupid)
egen multihouse = total(cplxhh) if inw4==1, by(hhid4)
recode multihouse (1=0) (2/10 = 1)

***Earnings*** 
// earnings from employment are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Earnings*** 
// earnings from employment are imputed at respondent level
*wave 4 respondent earnings from employment
gen r`wv'itearn = ydip
label variable r`wv'itearn  "r`wv'itearn:w`wv' Income: R Earnings from employment (after-tax)"

*wave 4 respondent flag for earnings from employment
imputation_flag ydip_f, gen(r`wv'itfearn)
label variable r`wv'itfearn "r`wv'itfearn:w`wv' IncFlag: R Earnings from employment (after-tax)"
label values r`wv'itfearn incflag

*wave 4 spouse earnings from employment
gen s`wv'itearn = .
spouse r`wv'itearn, result(s`wv'itearn) wave(`wv')
label variable s`wv'itearn  "s`wv'itearn:w`wv' Income: S Earnings from employment  (after-tax)"

*wave 4 spouse flag for earnings from employment
gen s`wv'itfearn = . 
spouse r`wv'itfearn, result(s`wv'itfearn) wave(`wv')
label variable s`wv'itfearn "s`wv'itfearn:w`wv' IncFlag: S Earnings from employment (after-tax)"
label values s`wv'itfearn incflag


***Capital Income***
// earnings from self-employment are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

// rental income is asked of the household respondent on behalf of 
// the whole household. The imputations for these questions are based
// the household respondent's answers and all members of the household
// are given the same value. Given this couple-level
// values can only be derived for couples without any additional household
// members.

// income from bank account and income from bonds, stocks, and mutal 
// funds are asked of the financial respondent on behalf of 
// the respondent and the spouse, if any spouse. The respondent and spouse
// can also choose to answer questions about their finanes separetly in which
// case both the respondent and the spouse answer asset questions. These values
// are imputed at the individual level and at the household level.
// Individual level imputations are available for all financial respondents. 
// All non financial respodents are assigned 0 values for the
// indvidual level asset imputation variables. 


**self-employment income
*wave 4 respondent earnings from self-employment
gen r`wv'itsemp = yind
label variable r`wv'itsemp "r`wv'itsemp:w`wv' Income: R Earnings from self-employment (after-tax)"

*wave 4 respondent flag for earnings from self-employment
imputation_flag yind_f, gen(r`wv'itfsemp)
label variable r`wv'itfsemp "r`wv'itfsemp:w`wv' IncFlag: R Earnings from self-employment (after-tax)"
label values r`wv'itfsemp incflag

*wave 4 spouse earnings from self-employment
gen s`wv'itsemp = .
spouse r`wv'itsemp, result(s`wv'itsemp) wave(`wv')
label variable s`wv'itsemp "s`wv'itsemp:w`wv' Income: S Earnings from self-employment (after-tax)"

*wave 4 spouse flag for earnings from self-employment
gen s`wv'itfsemp = . 
spouse r`wv'itfsemp, result(s`wv'itfsemp) wave(`wv')
label variable s`wv'itfsemp "s`wv'itfsemp:w`wv' IncFlag: S Earnings from self-employment (after-tax)"
label values s`wv'itfsemp incflag

*wave 4 couple-level earnings from self-employment
egen h`wv'itsemp = total(yind) if p_nrp == 0, by(h`wv'coupid)

imputation_flag yind_f, gen(h`wv'itfsemp) by(h`wv'coupid)
replace h`wv'itfsemp = -2 if p_nrp == 1

*wave 4 household-level earnings from self-employment
egen hh`wv'itsemp = total(yind) if p_nrp == 0, by(hh`wv'hhid)

imputation_flag yind_f, gen(hh`wv'itfsemp) by(hh`wv'hhid)
replace hh`wv'itfsemp = -2 if p_nrp == 1

**rental income***
*create more complete imputation flag for rental income
gen ysrent_hf = ysrent_f
replace ysrent_hf = 2 if ores_f == 2
replace ysrent_hf = 8 if ores_f == 8

*wave 4 couple-level rental income
gen h`wv'itrent = ysrent if multihouse == 0
label variable h`wv'itrent "h`wv'itrent:w`wv' Income: R+S Rental income (after-tax)"

imputation_flag ysrent_hf, gen(h`wv'itfrent) by(hh`wv'hhid)
replace h`wv'itfrent = -1 if multihouse == 1
label variable h`wv'itfrent "h`wv'itfrent:w`wv' Impflag: R+S Rental income (after-tax)"
label values h`wv'itfrent incflag

*wave 4 household-level rental income
gen hh`wv'itrent = ysrent
label variable hh`wv'itrent "hh`wv'itrent:w`wv' Income: HHold Rental income (after-tax)"

imputation_flag ysrent_hf, gen(hh`wv'itfrent) by(hh`wv'hhid)
label variable hh`wv'itfrent "hh`wv'itfrent:w`wv' Income: HHold Rental income (after-tax)"
label values hh`wv'itfrent incflag

drop ysrent_hf

**interest income from financial assets
*wave 4 couple-level income from bank account, bonds, stocks, and mutual funds
gen h`wv'ittrest = ybabsmf
label variable h`wv'ittrest "h`wv'ittrest:w`wv' Income: R+S Interest income from financial assets (after-tax)"

imputation_flag bacc_f bsmf_f, gen(h`wv'itftrest) by(h`wv'coupid) // ybabsmf_f does not provide detailed imputation information
label variable h`wv'itftrest "h`wv'itftrest:w`wv' Impflag: R+S Interest income from financial assets (after-tax)"
label values h`wv'itftrest incflag
replace h`wv'itftrest = 6 if h`wv'ittrest == 0

*wave 4 household-level income from bank account, bonds, stocks, and mutal funds
egen hh`wv'ittrest = total(ybabsmf * (cplxhh==1)), by(hh`wv'hhid) m
label variable hh`wv'ittrest "hh`wv'ittrest:w`wv' Income: HHold Interest income from financial assets (after-tax)"

imputation_flag bacc_f bsmf_f, gen(hh`wv'itftrest) by(hh`wv'hhid)
label variable hh`wv'itftrest "hh`wv'itftrest:w`wv' Impflag: HHold Interest income from financial assets (after-tax)"
label values hh`wv'itftrest incflag
replace hh`wv'itftrest = 6 if hh`wv'ittrest == 0

*wave 4 couple-level capital income
generate double h`wv'itcap  = h`wv'itsemp + h`wv'itrent + h`wv'ittrest
label variable h`wv'itcap  "h`wv'itcap:w`wv' Income: R+S Capital Income (after-tax)"

*wave 4 couple-level flag for capital income
h_imputation_flag h`wv'itfsemp h`wv'itfrent h`wv'itftrest, gen(h`wv'itfcap)
label variable h`wv'itfcap "h`wv'itfcap:w`wv' IncFlag: R+S Capital Inc (after-tax)"
label values h`wv'itfcap incflag

*wave 4 household-level capital income
generate double hh`wv'itcap = hh`wv'itsemp + hh`wv'itrent + hh`wv'ittrest
label variable hh`wv'itcap  "hh`wv'itcap:w`wv' Income: HHold Capital Income (after-tax)"

*wave 4 household-level flag for capital income
h_imputation_flag hh`wv'itfsemp hh`wv'itfrent hh`wv'itftrest, gen(hh`wv'itfcap)
label variable hh`wv'itfcap "hh`wv'itfcap:w`wv' IncFlag: HHold Capital Inc (after-tax)"
label values hh`wv'itfcap incflag

drop h`wv'itsemp hh`wv'itsemp
drop h`wv'itfsemp hh`wv'itfsemp

***Employer/Private Pension and Annuity***
// incomes from pensions are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

// incomes from other regular payments are asked in the employment 
// and pensions module to all respondents. The value is imputed 
// at the respondent level

***Employer/Private Pension and Annuity
*wave 4 respondent income from private and occupational pensions
gen r`wv'itpena = ypen2 + yreg1 + ylsp2 + ylsr1
label variable r`wv'itpena "r`wv'itpena:w`wv' Income: R Pension + Annuity (after-tax)"

*wave 4 respondent income flag from private and occupational pensions
imputation_flag ypen2_f yreg1_f ylsp2_f ylsr1_f, gen(r`wv'itfpena)
label variable r`wv'itfpena "r`wv'itfpena:w`wv' ImpFlag: R Pension + Annuity (after-tax)"
label values r`wv'itfpena incflag

*wave 4 spouse income from private and occupational pensions
gen s`wv'itpena = .
spouse r`wv'itpena, result(s`wv'itpena) wave(`wv')
label variable s`wv'itpena "s`wv'itpena:w`wv' Income: S Pension + Annuity (after-tax)"

*wave 4 spouse flag for income from private and occupational pensions
gen s`wv'itfpena = .
spouse r`wv'itfpena, result(s`wv'itfpena) wave(`wv')
label variable s`wv'itfpena "s`wv'itfpena:w`wv' ImpFlag: S Pension + Annuity (after-tax)"
label values s`wv'itfpena incflag

***Public Pensions***
// incomes from pensions are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Public Disability Pensions
*wave 4 respondent income from public disability pensions
gen r`wv'itssdi = ypen36 + ylsp36
label variable r`wv'itssdi "r`wv'itssdi:w`wv' Income: R Public Disability Pensions (after-tax)"

*wave 4 respondent flag for income from public disability pensions
imputation_flag ypen36_f ylsp36_f, gen(r`wv'itfssdi)
label variable r`wv'itfssdi "r`wv'itfssdi:w`wv' IncFlag: R Public Disability Pensions (after-tax)"
label values r`wv'itfssdi incflag

*wave 4 spouse income from public disability pensions
gen s`wv'itssdi = .
spouse r`wv'itssdi, result(s`wv'itssdi) wave(`wv')
label variable s`wv'itssdi "s`wv'itssdi:w`wv' Income: S Public Disability Pensions (after-tax)"

*wave 4 spouse flag for income from public disability pensions
gen s`wv'itfssdi = .
spouse r`wv'itfssdi, result(s`wv'itfssdi) wave(`wv')
label variable s`wv'itfssdi "s`wv'itfssdi:w`wv' IncFlag: S Public Disability Pensions (after-tax)"
label values s`wv'itfssdi incflag

***Public Old Age Pensions
*wave 4 respondent income from public old age + early retirement pensions
gen r`wv'itsret = ypen1 + ylsp1
label variable r`wv'itsret "r`wv'itsret:w`wv' Income: R Public Old Age + Early Retirement Pensions (after-tax)"

*wave 4 respondent flag for income from  public old age + early retirement pensions
imputation_flag ypen1_f ylsp1_f, gen(r`wv'itfsret)
label variable r`wv'itfsret "r`wv'itfsret:w`wv' IncFlag: R Public Old Age + Early Retirement Pensions (after-tax)"
label values r`wv'itfsret incflag

*wave 4 spouse income from public old age + early retirement pensions
gen s`wv'itsret = .
spouse r`wv'itsret, result(s`wv'itsret) wave(`wv')
label variable s`wv'itsret "s`wv'itsret:w`wv' Income: S Public Old Age + Early Retirement Pensions (after-tax)"

*wave 4 spouse flag for income from public old age + early retirement pensions
gen s`wv'itfsret = .
spouse r`wv'itfsret, result(s`wv'itfsret) wave(`wv')
label variable s`wv'itfsret "s`wv'itfsret:w`wv' IncFlag: S Public Old Age + Early Retirement Pensions (after-tax)"
label values s`wv'itfssdi incflag

***Total Public Pensions
*wave 4 respondent income from total public pensions
gen r`wv'itpubpen = r`wv'itssdi + r`wv'itsret
label variable r`wv'itpubpen "r`wv'itpubpen:w`wv' Income: R Public Pensions (after-tax)"

*wave 4 respondent flag for income from total public pensions
h_imputation_flag r`wv'itfssdi r`wv'itfsret, gen(r`wv'itfpubpen)
label variable r`wv'itfpubpen "r`wv'itfpubpen:w`wv' Impflag: R Public Pensions (after-tax)"
label values r`wv'itfpubpen incflag

*wave 4 spouse income from total public pensions
gen s`wv'itpubpen = .
spouse r`wv'itpubpen, result(s`wv'itpubpen) wave(`wv')
label variable s`wv'itpubpen "s`wv'itpubpen:w`wv' Income: S Public Pensions (after-tax)"

*wave 4 spouse flag for income from total public pensions
gen s`wv'itfpubpen = .
spouse r`wv'itfpubpen, result(s`wv'itfpubpen) wave(`wv')
label variable s`wv'itfpubpen "s`wv'itfpubpen:w`wv' IncFlag: S Public Pensions (after-tax)"
label values s`wv'itfpubpen incflag

***Other Government Transfers***
// incomes from other goverment transfers are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Other Government Transfers
*wave 4 respondent income from Other Government Transfers
gen r`wv'itgxfr = ypen4 + ypen5 + ylsp4 + ylsp5
label variable r`wv'itgxfr "r`wv'itgxfr:w`wv' Income: R Other Government Transfers (after-tax)"

*wave 4 respondent flag for income from Other Government Transfers
imputation_flag ypen4_f ypen5_f ylsp4_f ylsp5_f, gen(r`wv'itfgxfr)
label variable r`wv'itfgxfr "r`wv'itfgxfr:w`wv' IncFlag: R Other Government Transfers (after-tax)"
label values r`wv'itfgxfr incflag

*wave 4 spouse income from Other Government Transfers
gen s`wv'itgxfr = .
spouse r`wv'itgxfr, result(s`wv'itgxfr) wave(`wv')
label variable s`wv'itgxfr "s`wv'itgxfr:w`wv' Income: S Other Government Transfers (after-tax)"

*wave 4 spouse flag for income from Other Government Transfers
gen s`wv'itfgxfr = .
spouse r`wv'itfgxfr, result(s`wv'itfgxfr) wave(`wv')
label variable s`wv'itfgxfr "s`wv'itfgxfr:w`wv' IncFlag: S Other Government Transfers (after-tax)"
label values s`wv'itfgxfr incflag

***Other Income***
// incomes from other regular payments are asked in the employment 
// and pensions module to all respondents. The value is imputed 
// at the respondent level

*wave 4 respondent income from other income
gen r`wv'itothr =  yreg2 + ylsr2
label variable r`wv'itothr "r`wv'itothr:w`wv' Income: R Other Income (after-tax)"

*wave 4 respondent flag for income from other income
imputation_flag yreg2_f ylsr2_f, gen(r`wv'itfothr)
label variable r`wv'itfothr "r`wv'itfothr:w`wv' IncFlag: R Other Income (after-tax)"
label values r`wv'itfothr incflag

*wave 4 spouse income from other income
gen s`wv'itothr = .
spouse r`wv'itothr, result(s`wv'itothr) wave(`wv')
label variable s`wv'itothr "s`wv'itothr:w`wv' Income: S Other Income (after-tax)"

*wave 4 spouse flag for income from other income
gen s`wv'itfothr = .
spouse r`wv'itfothr, result(s`wv'itfothr) wave(`wv')
label variable s`wv'itfothr "s`wv'itfothr:w`wv' IncFlag: S Other Income (after-tax)"
label values s`wv'itfothr incflag

***Income from other household members***
// income from other household members is imputed at full-household level

***Income from other household members
*wave 4 household-level income from other household members
gen hh`wv'itothhh = yaohm
label variable hh`wv'itothhh "hh`wv'itothhh:w`wv' Income: HHold Other Household Members' Inc (after-tax)"

imputation_flag yaohm_f, gen(hh`wv'itfothhh) by(hh`wv'hhid)
label variable hh`wv'itfothhh "hh`wv'itfothhh:w`wv' Impflag: HHold Other Household Members' Inc (after-tax)"
label values hh`wv'itfothhh incflag

***Total Income***

*couple-level earnings from employment
egen h`wv'itearn = total(r`wv'itearn) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfearn, gen(h`wv'itfearn) by(h`wv'coupid)
replace h`wv'itfearn = -2 if p_nrp == 1

*couple-level income from private pensions
egen h`wv'itpena = total(r`wv'itpena) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfpena, gen(h`wv'itfpena) by(h`wv'coupid)
replace h`wv'itfpena = -2 if p_nrp == 1

*couple-level income from public pensions
egen h`wv'itpubpen = total(r`wv'itpubpen) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfpubpen, gen(h`wv'itfpubpen) by(h`wv'coupid)
replace h`wv'itfpubpen = -2 if p_nrp == 1

*couple-level income from goverment transfers
egen h`wv'itgxfr = total(r`wv'itgxfr) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfgxfr, gen(h`wv'itfgxfr) by(h`wv'coupid)
replace h`wv'itfgxfr = -2 if p_nrp == 1

*couple-level income from other income
egen h`wv'itothr = total(r`wv'itothr) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfothr, gen(h`wv'itfothr) by(h`wv'coupid)
replace h`wv'itfothr = -2 if p_nrp == 1

*household-level earnings from employment
egen hh`wv'itearn = total(r`wv'itearn) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfearn, gen(hh`wv'itfearn) by(hh`wv'hhid)
replace hh`wv'itfearn = -2 if p_nrp == 1

*household-level income from private pensions
egen hh`wv'itpena = total(r`wv'itpena) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfpena, gen(hh`wv'itfpena) by(hh`wv'hhid)
replace hh`wv'itfpena = -2 if p_nrp == 1

*household-level income from public pensions
egen hh`wv'itpubpen = total(r`wv'itpubpen) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfpubpen, gen(hh`wv'itfpubpen) by(hh`wv'hhid)
replace hh`wv'itfpubpen = -2 if p_nrp == 1

*household-level income from government transfers
egen hh`wv'itgxfr = total(r`wv'itgxfr) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfgxfr, gen(hh`wv'itfgxfr) by(hh`wv'hhid)
replace hh`wv'itfgxfr = -2 if p_nrp == 1

*household-level income from other income
egen hh`wv'itothr = total(r`wv'itothr) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfothr, gen(hh`wv'itfothr) by(hh`wv'hhid)
replace hh`wv'itfothr = -2 if p_nrp == 1

*household-level total income
gen hh`wv'ittotinc = thinc

imputation_flag thinc_f, gen(hh`wv'itftotinc) by(hh`wv'hhid)

*wave 4 couple-level total income
generate double h`wv'ittot = h`wv'itearn + h`wv'itcap + h`wv'itpena + h`wv'itpubpen + h`wv'itgxfr + h`wv'itothr
label variable h`wv'ittot "h`wv'ittot:w`wv' Incm: R+S Total Income (after-tax)"

*wave 4 couple-level flag for total income
h_imputation_flag h`wv'itfearn h`wv'itfcap h`wv'itfpena h`wv'itfpubpen h`wv'itfgxfr h`wv'itfothr, gen(h`wv'itftot)
label variable h`wv'itftot "h`wv'itftot:w`wv' IncFlag: R+S Total Inc (after-tax)"
label values h`wv'itftot incflag

*wave 4 household-level total income
generate double hh`wv'ittot = hh`wv'ittotinc
label variable hh`wv'ittot "hh`wv'ittot:w`wv' Incm: HHold Total Income (after-tax)"

*wave 4 household-level flag for total income
h_imputation_flag hh`wv'itfearn hh`wv'itfcap hh`wv'itfpena hh`wv'itfpubpen hh`wv'itfgxfr hh`wv'itfothr hh`wv'itfothhh, gen(hh`wv'itftot)
replace hh`wv'itftot = -2 if hh`wv'itftotinc == 9
label variable hh`wv'itftot "hh`wv'itftot:w`wv' IncFlag: HHold Total Income (after-tax)"
label values hh`wv'itftot incflag

drop h`wv'itearn  h`wv'itpena  h`wv'itpubpen  h`wv'itothr h`wv'itgxfr ///
		 h`wv'itfearn h`wv'itfpena h`wv'itfpubpen h`wv'itfothr h`wv'itfgxfr ///
		 hh`wv'itearn  hh`wv'itpena  hh`wv'itpubpen  hh`wv'itothr hh`wv'itgxfr hh`wv'ittotinc  ///
		 hh`wv'itfearn hh`wv'itfpena hh`wv'itfpubpen hh`wv'itfothr hh`wv'itfgxfr hh`wv'itftotinc
		 
drop cplxhh multihouse

***Consumption: food at home***
*wave 4 household consumption food at home
***********don't know, refuse possibly very high positive number
gen foodh = .
replace foodh = .m if co011e > 2000000
replace foodh = 0 if co010_ == 5
replace foodh = co011e if inrange(co011e,0,2000000) & co010_ == 1
gen foodi = .
replace foodi = .m if co002e > 2000000
replace foodi = co002e if inrange(co002e,0,2000000)
gen hh`wv'cfoodi1m = .
replace hh`wv'cfoodi1m = .m if (co002e == . | co011e == . | co010_ == . | foodh == .m | foodi == .m) & inw`wv' == 1
replace hh`wv'cfoodi1m = .r if co002e == -9999992 | co010_ == -9999992 | co011e == -9999992
replace hh`wv'cfoodi1m = .d if co002e == -9999991 | co010_ == -9999991 | co011e == -9999991
replace hh`wv'cfoodi1m = foodh + foodi if inrange(foodh,0,2000000) & inrange(foodi,0,2000000)
label variable hh`wv'cfoodi1m "hh`wv'cfoodi1m:w`wv' hhold monthly food consumption in house"

bysort h`wv'coupid: replace hh`wv'cfoodi1m = hh`wv'cfoodi1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfoodi1m = hh`wv'cfoodi1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

drop foodh foodi

***Consumption: food outside home***
*wave 4 household consumption food outside home
gen hh`wv'cfoodo1m = .
replace hh`wv'cfoodo1m = .m if (co003e == . | co003e > 2000000 | co003e < 0) & inw`wv' == 1
replace hh`wv'cfoodo1m = .r if co003e == -9999992
replace hh`wv'cfoodo1m = .d if co003e == -9999991
replace hh`wv'cfoodo1m = co003e if inrange(co003e,0,2000000)
label variable hh`wv'cfoodo1m "hh`wv'cfoodo1m:w`wv' hhold monthly food consumption outside house"

bysort h`wv'coupid: replace hh`wv'cfoodo1m = hh`wv'cfoodo1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfoodo1m = hh`wv'cfoodo1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

***Consumption: total food***
*wave 4 household consumption total food
gen hh`wv'cfood1m = .
replace hh`wv'cfood1m = .m if hh`wv'cfoodi1m == .m | hh`wv'cfoodo1m == .m
replace hh`wv'cfood1m = .r if co002e == -9999992 | co003e == -9999992 | co011e == -9999992 | co010_ == -9999992
replace hh`wv'cfood1m = .d if co002e == -9999991 | co003e == -9999991 | co011e == -9999991 | co010_ == -9999991
replace hh`wv'cfood1m = hh`wv'cfoodi1m + hh`wv'cfoodo1m if inrange(hh`wv'cfoodi1m,0,4000000) & inrange(hh`wv'cfoodo1m,0,2000000)
label variable hh`wv'cfood1m "hh`wv'cfood1m:w`wv' hhold total monthly food consumption"

bysort h`wv'coupid: replace hh`wv'cfood1m = hh`wv'cfood1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfood1m = hh`wv'cfood1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse


***drop ELSA wave 4 financial derived file raw variables***
drop `income_w4_imput'

***drop wave 4 consumption file raw variables***
drop `income_w4_co'

******************************************************************************************

*set wave number
local wv = 5

***merge with wave 5 imputation data***
local income_w5_imput ysrent ybabsmf   ///
                      ysrent_f ybabsmf_f ///
                      ypen1 ypen2 ypen36 ypen4 ypen5 ///
                      ypen1_f ypen2_f ypen36_f ypen4_f ypen5_f ///
                      ylsp1 ylsp2 ylsp36 ylsp4 ylsp5 ///
                      ylsp1_f ylsp2_f ylsp36_f ylsp4_f ylsp5_f ///
                      yreg1 yreg2 ///
                      yreg1_f yreg2_f ///
                      ylsr1 ylsr2 ///
                      ylsr1_f ylsr2_f ///
                      yind ydip yind_f ydip_f  ///
                      ores_f  ///
                      yaohm yaohm_f thinc thinc_f p_nrp 
merge 1:1 mergeid using "$wave_5_imputations_", keepusing(`income_w5_imput') nogen

***merge with wave 5 consumption data***
local income_w5_co co002e co003e co010_ co011e 
merge 1:1 mergeid using "$wave_5_co", keepusing(`income_w5_co') nogen


***create indicator of multiple couples in household
egen cplxhh = tag(hhid5 h5coupid)
egen multihouse = total(cplxhh) if inw5==1, by(hhid5)
recode multihouse (1=0) (2/10 = 1)

***Earnings*** 
// earnings from employment are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Earnings*** 
// earnings from employment are imputed at respondent level
*wave 5 respondent earnings from employment
gen r`wv'itearn = ydip
label variable r`wv'itearn  "r`wv'itearn:w`wv' Income: R Earnings from employment (after-tax)"

*wave 5 respondent flag for earnings from employment
imputation_flag ydip_f, gen(r`wv'itfearn)
label variable r`wv'itfearn "r`wv'itfearn:w`wv' IncFlag: R Earnings from employment (after-tax)"
label values r`wv'itfearn incflag

*wave 5 spouse earnings from employment
gen s`wv'itearn = .
spouse r`wv'itearn, result(s`wv'itearn) wave(`wv')
label variable s`wv'itearn  "s`wv'itearn:w`wv' Income: S Earnings from employment  (after-tax)"

*wave 5 spouse flag for earnings from employment
gen s`wv'itfearn = . 
spouse r`wv'itfearn, result(s`wv'itfearn) wave(`wv')
label variable s`wv'itfearn "s`wv'itfearn:w`wv' IncFlag: S Earnings from employment (after-tax)"
label values s`wv'itfearn incflag


***Capital Income***
// earnings from self-employment are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

// rental income is asked of the household respondent on behalf of 
// the whole household. The imputations for these questions are based
// the household respondent's answers and all members of the household
// are given the same value. Given this couple-level
// values can only be derived for couples without any additional household
// members.

// income from bank account and income from bonds, stocks, and mutal 
// funds are asked of the financial respondent on behalf of 
// the respondent and the spouse, if any spouse. The respondent and spouse
// can also choose to answer questions about their finanes separetly in which
// case both the respondent and the spouse answer asset questions. These values
// are imputed at the individual level and at the household level.
// Individual level imputations are available for all financial respondents. 
// All non financial respodents are assigned 0 values for the
// indvidual level asset imputation variables. 


**self-employment income
*wave 5 respondent earnings from self-employment
gen r`wv'itsemp = yind
label variable r`wv'itsemp "r`wv'itsemp:w`wv' Income: R Earnings from self-employment (after-tax)"

*wave 5 respondent flag for earnings from self-employment
imputation_flag yind_f, gen(r`wv'itfsemp)
label variable r`wv'itfsemp "r`wv'itfsemp:w`wv' IncFlag: R Earnings from self-employment (after-tax)"
label values r`wv'itfsemp incflag

*wave 5 spouse earnings from self-employment
gen s`wv'itsemp = .
spouse r`wv'itsemp, result(s`wv'itsemp) wave(`wv')
label variable s`wv'itsemp "s`wv'itsemp:w`wv' Income: S Earnings from self-employment (after-tax)"

*wave 5 spouse flag for earnings from self-employment
gen s`wv'itfsemp = . 
spouse r`wv'itfsemp, result(s`wv'itfsemp) wave(`wv')
label variable s`wv'itfsemp "s`wv'itfsemp:w`wv' IncFlag: S Earnings from self-employment (after-tax)"
label values s`wv'itfsemp incflag

*wave 5 couple-level earnings from self-employment
egen h`wv'itsemp = total(yind) if p_nrp == 0, by(h`wv'coupid)

imputation_flag yind_f, gen(h`wv'itfsemp) by(h`wv'coupid)
replace h`wv'itfsemp = -2 if p_nrp == 1

*wave 5 household-level earnings from self-employment
egen hh`wv'itsemp = total(yind) if p_nrp == 0, by(hh`wv'hhid)

imputation_flag yind_f, gen(hh`wv'itfsemp) by(hh`wv'hhid)
replace hh`wv'itfsemp = -2 if p_nrp == 1

**rental income***
*create more complete imputation flag for rental income
gen ysrent_hf = ysrent_f
replace ysrent_hf = 2 if ores_f == 2
replace ysrent_hf = 8 if ores_f == 8

*wave 5 couple-level rental income
gen h`wv'itrent = ysrent if multihouse == 0
label variable h`wv'itrent "h`wv'itrent:w`wv' Income: R+S Rental income (after-tax)"

imputation_flag ysrent_hf, gen(h`wv'itfrent) by(hh`wv'hhid)
replace h`wv'itfrent = -1 if multihouse == 1
label variable h`wv'itfrent "h`wv'itfrent:w`wv' Impflag: R+S Rental income (after-tax)"
label values h`wv'itfrent incflag

*wave 5 household-level rental income
gen hh`wv'itrent = ysrent
label variable hh`wv'itrent "hh`wv'itrent:w`wv' Income: HHold Rental income (after-tax)"

imputation_flag ysrent_hf, gen(hh`wv'itfrent) by(hh`wv'hhid)
label variable hh`wv'itfrent "hh`wv'itfrent:w`wv' Income: HHold Rental income (after-tax)"
label values hh`wv'itfrent incflag

drop ysrent_hf

**interest income from financial assets
*wave 5 couple-level income from bank account, bonds, stocks, and mutual funds
gen h`wv'ittrest = ybabsmf
label variable h`wv'ittrest "h`wv'ittrest:w`wv' Income: R+S Interest income from financial assets (after-tax)"

imputation_flag ybabsmf_f, gen(h`wv'itftrest) by(h`wv'coupid)
label variable h`wv'itftrest "h`wv'itftrest:w`wv' Impflag: R+S Interest income from financial assets (after-tax)"
label values h`wv'itftrest incflag

*wave 5 household-level income from bank account, bonds, stocks, and mutal funds
egen hh`wv'ittrest = total(ybabsmf * (cplxhh==1)), by(hh`wv'hhid) m
label variable hh`wv'ittrest "hh`wv'ittrest:w`wv' Income: HHold Interest income from financial assets (after-tax)"

imputation_flag ybabsmf_f, gen(hh`wv'itftrest) by(hh`wv'hhid)
label variable hh`wv'itftrest "hh`wv'itftrest:w`wv' Impflag: HHold Interest income from financial assets (after-tax)"
label values hh`wv'itftrest incflag

*wave 5 couple-level capital income
generate double h`wv'itcap  = h`wv'itsemp + h`wv'itrent + h`wv'ittrest
label variable h`wv'itcap  "h`wv'itcap:w`wv' Income: R+S Capital Income (after-tax)"

*wave 5 couple-level flag for capital income
h_imputation_flag h`wv'itfsemp h`wv'itfrent h`wv'itftrest, gen(h`wv'itfcap)
label variable h`wv'itfcap "h`wv'itfcap:w`wv' IncFlag: R+S Capital Inc (after-tax)"
label values h`wv'itfcap incflag

*wave 5 household-level capital income
generate double hh`wv'itcap = hh`wv'itsemp + hh`wv'itrent + hh`wv'ittrest
label variable hh`wv'itcap  "hh`wv'itcap:w`wv' Income: HHold Capital Income (after-tax)"

*wave 5 household-level flag for capital income
h_imputation_flag hh`wv'itfsemp hh`wv'itfrent hh`wv'itftrest, gen(hh`wv'itfcap)
label variable hh`wv'itfcap "hh`wv'itfcap:w`wv' IncFlag: HHold Capital Inc (after-tax)"
label values hh`wv'itfcap incflag

drop h`wv'itsemp hh`wv'itsemp
drop h`wv'itfsemp hh`wv'itfsemp

***Employer/Private Pension and Annuity***
// incomes from pensions are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

// incomes from other regular payments are asked in the employment 
// and pensions module to all respondents. The value is imputed 
// at the respondent level

***Employer/Private Pension and Annuity
*wave 5 respondent income from private and occupational pensions
gen r`wv'itpena = ypen2 + yreg1 + ylsp2 + ylsr1
label variable r`wv'itpena "r`wv'itpena:w`wv' Income: R Pension + Annuity (after-tax)"

*wave 5 respondent income flag from private and occupational pensions
imputation_flag ypen2_f yreg1_f ylsp2_f ylsr1_f, gen(r`wv'itfpena)
label variable r`wv'itfpena "r`wv'itfpena:w`wv' ImpFlag: R Pension + Annuity (after-tax)"
label values r`wv'itfpena incflag

*wave 5 spouse income from private and occupational pensions
gen s`wv'itpena = .
spouse r`wv'itpena, result(s`wv'itpena) wave(`wv')
label variable s`wv'itpena "s`wv'itpena:w`wv' Income: S Pension + Annuity (after-tax)"

*wave 5 spouse flag for income from private and occupational pensions
gen s`wv'itfpena = .
spouse r`wv'itfpena, result(s`wv'itfpena) wave(`wv')
label variable s`wv'itfpena "s`wv'itfpena:w`wv' ImpFlag: S Pension + Annuity (after-tax)"
label values s`wv'itfpena incflag

***Public Pensions***
// incomes from pensions are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Public Disability Pensions
*wave 5 respondent income from public disability pensions
gen r`wv'itssdi = ypen36 + ylsp36
label variable r`wv'itssdi "r`wv'itssdi:w`wv' Income: R Public Disability Pensions (after-tax)"

*wave 5 respondent flag for income from public disability pensions
imputation_flag ypen36_f ylsp36_f, gen(r`wv'itfssdi)
label variable r`wv'itfssdi "r`wv'itfssdi:w`wv' IncFlag: R Public Disability Pensions (after-tax)"
label values r`wv'itfssdi incflag

*wave 5 spouse income from public disability pensions
gen s`wv'itssdi = .
spouse r`wv'itssdi, result(s`wv'itssdi) wave(`wv')
label variable s`wv'itssdi "s`wv'itssdi:w`wv' Income: S Public Disability Pensions (after-tax)"

*wave 5 spouse flag for income from public disability pensions
gen s`wv'itfssdi = .
spouse r`wv'itfssdi, result(s`wv'itfssdi) wave(`wv')
label variable s`wv'itfssdi "s`wv'itfssdi:w`wv' IncFlag: S Public Disability Pensions (after-tax)"
label values s`wv'itfssdi incflag

***Public Old Age Pensions
*wave 5 respondent income from public old age + early retirement pensions
gen r`wv'itsret = ypen1 + ylsp1
label variable r`wv'itsret "r`wv'itsret:w`wv' Income: R Public Old Age + Early Retirement Pensions (after-tax)"

*wave 5 respondent flag for income from  public old age + early retirement pensions
imputation_flag ypen1_f ylsp1_f, gen(r`wv'itfsret)
label variable r`wv'itfsret "r`wv'itfsret:w`wv' IncFlag: R Public Old Age + Early Retirement Pensions (after-tax)"
label values r`wv'itfsret incflag

*wave 5 spouse income from public old age + early retirement pensions
gen s`wv'itsret = .
spouse r`wv'itsret, result(s`wv'itsret) wave(`wv')
label variable s`wv'itsret "s`wv'itsret:w`wv' Income: S Public Old Age + Early Retirement Pensions (after-tax)"

*wave 5 spouse flag for income from public old age + early retirement pensions
gen s`wv'itfsret = .
spouse r`wv'itfsret, result(s`wv'itfsret) wave(`wv')
label variable s`wv'itfsret "s`wv'itfsret:w`wv' IncFlag: S Public Old Age + Early Retirement Pensions (after-tax)"
label values s`wv'itfssdi incflag

***Total Public Pensions
*wave 5 respondent income from total public pensions
gen r`wv'itpubpen = r`wv'itssdi + r`wv'itsret
label variable r`wv'itpubpen "r`wv'itpubpen:w`wv' Income: R Public Pensions (after-tax)"

*wave 5 respondent flag for income from total public pensions
h_imputation_flag r`wv'itfssdi r`wv'itfsret, gen(r`wv'itfpubpen)
label variable r`wv'itfpubpen "r`wv'itfpubpen:w`wv' Impflag: R Public Pensions (after-tax)"
label values r`wv'itfpubpen incflag

*wave 5 spouse income from total public pensions
gen s`wv'itpubpen = .
spouse r`wv'itpubpen, result(s`wv'itpubpen) wave(`wv')
label variable s`wv'itpubpen "s`wv'itpubpen:w`wv' Income: S Public Pensions (after-tax)"

*wave 5 spouse flag for income from total public pensions
gen s`wv'itfpubpen = .
spouse r`wv'itfpubpen, result(s`wv'itfpubpen) wave(`wv')
label variable s`wv'itfpubpen "s`wv'itfpubpen:w`wv' IncFlag: S Public Pensions (after-tax)"
label values s`wv'itfpubpen incflag

***Other Government Transfers***
// incomes from other goverment transfers are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Other Government Transfers
*wave 5 respondent income from Other Government Transfers
gen r`wv'itgxfr = ypen4 + ypen5 + ylsp4 + ylsp5
label variable r`wv'itgxfr "r`wv'itgxfr:w`wv' Income: R Other Government Transfers (after-tax)"

*wave 5 respondent flag for income from Other Government Transfers
imputation_flag ypen4_f ypen5_f ylsp4_f ylsp5_f, gen(r`wv'itfgxfr)
label variable r`wv'itfgxfr "r`wv'itfgxfr:w`wv' IncFlag: R Other Government Transfers (after-tax)"
label values r`wv'itfgxfr incflag

*wave 5 spouse income from Other Government Transfers
gen s`wv'itgxfr = .
spouse r`wv'itgxfr, result(s`wv'itgxfr) wave(`wv')
label variable s`wv'itgxfr "s`wv'itgxfr:w`wv' Income: S Other Government Transfers (after-tax)"

*wave 5 spouse flag for income from Other Government Transfers
gen s`wv'itfgxfr = .
spouse r`wv'itfgxfr, result(s`wv'itfgxfr) wave(`wv')
label variable s`wv'itfgxfr "s`wv'itfgxfr:w`wv' IncFlag: S Other Government Transfers (after-tax)"
label values s`wv'itfgxfr incflag

***Other Income***
// incomes from other regular payments are asked in the employment 
// and pensions module to all respondents. The value is imputed 
// at the respondent level

*wave 5 respondent income from other income
gen r`wv'itothr =  yreg2 + ylsr2
label variable r`wv'itothr "r`wv'itothr:w`wv' Income: R Other Income (after-tax)"

*wave 5 respondent flag for income from other income
imputation_flag yreg2_f ylsr2_f, gen(r`wv'itfothr)
label variable r`wv'itfothr "r`wv'itfothr:w`wv' IncFlag: R Other Income (after-tax)"
label values r`wv'itfothr incflag

*wave 5 spouse income from other income
gen s`wv'itothr = .
spouse r`wv'itothr, result(s`wv'itothr) wave(`wv')
label variable s`wv'itothr "s`wv'itothr:w`wv' Income: S Other Income (after-tax)"

*wave 5 spouse flag for income from other income
gen s`wv'itfothr = .
spouse r`wv'itfothr, result(s`wv'itfothr) wave(`wv')
label variable s`wv'itfothr "s`wv'itfothr:w`wv' IncFlag: S Other Income (after-tax)"
label values s`wv'itfothr incflag

***Income from other household members***
// income from other household members is imputed at full-household level

***Income from other household members
*wave 5 household-level income from other household members
gen hh`wv'itothhh = yaohm
label variable hh`wv'itothhh "hh`wv'itothhh:w`wv' Income: HHold Other Household Members' Inc (after-tax)"

imputation_flag yaohm_f, gen(hh`wv'itfothhh) by(hh`wv'hhid)
label variable hh`wv'itfothhh "hh`wv'itfothhh:w`wv' Impflag: HHold Other Household Members' Inc (after-tax)"
label values hh`wv'itfothhh incflag

***Total Income***

*couple-level earnings from employment
egen h`wv'itearn = total(r`wv'itearn) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfearn, gen(h`wv'itfearn) by(h`wv'coupid)
replace h`wv'itfearn = -2 if p_nrp == 1

*couple-level income from private pensions
egen h`wv'itpena = total(r`wv'itpena) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfpena, gen(h`wv'itfpena) by(h`wv'coupid)
replace h`wv'itfpena = -2 if p_nrp == 1

*couple-level income from public pensions
egen h`wv'itpubpen = total(r`wv'itpubpen) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfpubpen, gen(h`wv'itfpubpen) by(h`wv'coupid)
replace h`wv'itfpubpen = -2 if p_nrp == 1

*couple-level income from goverment transfers
egen h`wv'itgxfr = total(r`wv'itgxfr) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfgxfr, gen(h`wv'itfgxfr) by(h`wv'coupid)
replace h`wv'itfgxfr = -2 if p_nrp == 1

*couple-level income from other income
egen h`wv'itothr = total(r`wv'itothr) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfothr, gen(h`wv'itfothr) by(h`wv'coupid)
replace h`wv'itfothr = -2 if p_nrp == 1

*household-level earnings from employment
egen hh`wv'itearn = total(r`wv'itearn) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfearn, gen(hh`wv'itfearn) by(hh`wv'hhid)
replace hh`wv'itfearn = -2 if p_nrp == 1

*household-level income from private pensions
egen hh`wv'itpena = total(r`wv'itpena) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfpena, gen(hh`wv'itfpena) by(hh`wv'hhid)
replace hh`wv'itfpena = -2 if p_nrp == 1

*household-level income from public pensions
egen hh`wv'itpubpen = total(r`wv'itpubpen) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfpubpen, gen(hh`wv'itfpubpen) by(hh`wv'hhid)
replace hh`wv'itfpubpen = -2 if p_nrp == 1

*household-level income from government transfers
egen hh`wv'itgxfr = total(r`wv'itgxfr) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfgxfr, gen(hh`wv'itfgxfr) by(hh`wv'hhid)
replace hh`wv'itfgxfr = -2 if p_nrp == 1

*household-level income from other income
egen hh`wv'itothr = total(r`wv'itothr) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfothr, gen(hh`wv'itfothr) by(hh`wv'hhid)
replace hh`wv'itfothr = -2 if p_nrp == 1

*household-level total income
gen hh`wv'ittotinc = thinc

imputation_flag thinc_f, gen(hh`wv'itftotinc) by(hh`wv'hhid)

*wave 5 couple-level total income
generate double h`wv'ittot = h`wv'itearn + h`wv'itcap + h`wv'itpena + h`wv'itpubpen + h`wv'itgxfr + h`wv'itothr
label variable h`wv'ittot "h`wv'ittot:w`wv' Incm: R+S Total Income (after-tax)"

*wave 5 couple-level flag for total income
h_imputation_flag h`wv'itfearn h`wv'itfcap h`wv'itfpena h`wv'itfpubpen h`wv'itfgxfr h`wv'itfothr, gen(h`wv'itftot)
label variable h`wv'itftot "h`wv'itftot:w`wv' IncFlag: R+S Total Inc (after-tax)"
label values h`wv'itftot incflag

*wave 5 household-level total income
generate double hh`wv'ittot = hh`wv'ittotinc
label variable hh`wv'ittot "hh`wv'ittot:w`wv' Incm: HHold Total Income (after-tax)"

*wave 5 household-level flag for total income
h_imputation_flag hh`wv'itfearn hh`wv'itfcap hh`wv'itfpena hh`wv'itfpubpen hh`wv'itfgxfr hh`wv'itfothr hh`wv'itfothhh, gen(hh`wv'itftot)
replace hh`wv'itftot = -2 if hh`wv'itftotinc == 9
label variable hh`wv'itftot "hh`wv'itftot:w`wv' IncFlag: HHold Total Income (after-tax)"
label values hh`wv'itftot incflag

drop h`wv'itearn  h`wv'itpena  h`wv'itpubpen  h`wv'itothr h`wv'itgxfr ///
		 h`wv'itfearn h`wv'itfpena h`wv'itfpubpen h`wv'itfothr h`wv'itfgxfr ///
		 hh`wv'itearn  hh`wv'itpena  hh`wv'itpubpen  hh`wv'itothr hh`wv'itgxfr hh`wv'ittotinc  ///
		 hh`wv'itfearn hh`wv'itfpena hh`wv'itfpubpen hh`wv'itfothr hh`wv'itfgxfr hh`wv'itftotinc
		 
drop cplxhh multihouse

***Consumption: food at home***
*wave 5 household consumption food at home
gen foodh = .
replace foodh = .m if (co011e > 2000000 | co011e < 0) & inw`wv' == 1
replace foodh = 0 if co010_ == 5
replace foodh = co011e if inrange(co011e,0,2000000) & co010_ == 1
gen foodi = .
replace foodi = .m if (co002e > 2000000 | co002e < 0) & inw`wv' == 1
replace foodi = co002e if inrange(co002e,0,2000000)
gen hh`wv'cfoodi1m = .
replace hh`wv'cfoodi1m = .m if (co002e == . | co011e == . | co010_ == . | foodh == .m | foodi == .m) & inw`wv' == 1
replace hh`wv'cfoodi1m = .r if co002e == -9999992 | co010_ == -9999992 | co011e == -9999992
replace hh`wv'cfoodi1m = .d if co002e == -9999991 | co010_ == -9999991 | co011e == -9999991
replace hh`wv'cfoodi1m = foodh + foodi if inrange(foodh,0,2000000) & inrange(foodi,0,2000000)
label variable hh`wv'cfoodi1m "hh`wv'cfoodi1m:w`wv' hhold monthly food consumption in house"

bysort h`wv'coupid: replace hh`wv'cfoodi1m = hh`wv'cfoodi1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfoodi1m = hh`wv'cfoodi1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

drop foodh foodi

***Consumption: food outside home***
*wave 5 household consumption food outside home
gen hh`wv'cfoodo1m = .
replace hh`wv'cfoodo1m = .m if (co003e == . | co003e > 2000000 | co003e < 0) & inw`wv' == 1
replace hh`wv'cfoodo1m = .r if co003e == -9999992
replace hh`wv'cfoodo1m = .d if co003e == -9999991
replace hh`wv'cfoodo1m = co003e if inrange(co003e,0,2000000)
label variable hh`wv'cfoodo1m "hh`wv'cfoodo1m:w`wv' hhold monthly food consumption outside house"

bysort h`wv'coupid: replace hh`wv'cfoodo1m = hh`wv'cfoodo1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfoodo1m = hh`wv'cfoodo1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

***Consumption: total food***
*wave 5 household consumption total food
gen hh`wv'cfood1m = .
replace hh`wv'cfood1m = .m if hh`wv'cfoodi1m == .m | hh`wv'cfoodo1m == .m
replace hh`wv'cfood1m = .r if co002e == -9999992 | co003e == -9999992 | co011e == -9999992 | co010_ == -9999992
replace hh`wv'cfood1m = .d if co002e == -9999991 | co003e == -9999991 | co011e == -9999991 | co010_ == -9999991
replace hh`wv'cfood1m = hh`wv'cfoodi1m + hh`wv'cfoodo1m if inrange(hh`wv'cfoodi1m,0,4000000) & inrange(hh`wv'cfoodo1m,0,2000000)
label variable hh`wv'cfood1m "hh`wv'cfood1m:w`wv' hhold total monthly food consumption"

bysort h`wv'coupid: replace hh`wv'cfood1m = hh`wv'cfood1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfood1m = hh`wv'cfood1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse


***drop SHARE wave 5 financial derived file raw variables***
drop `income_w5_imput'

***drop wave 5 consumption file raw variables***
drop `income_w5_co'

******************************************************************************************

*set wave number
local wv = 6

***merge with wave 6 imputation data***
local income_w6_imput ysrent ybabsmf   ///
                      ysrent_f ybabsmf_f ///
                      ypen1 ypen2 ypen3 ypen4 ypen5 ///
                      ypen1_f ypen2_f ypen3_f ypen4_f ypen5_f ///
                      ylsp1 ylsp2 ylsp3 ylsp4 ylsp5 ///
                      ylsp1_f ylsp2_f ylsp3_f ylsp4_f ylsp5_f ///
                      yreg1 yreg2 ///
                      yreg1_f yreg2_f ///
                      ylsr1 ylsr2 ///
                      ylsr1_f ylsr2_f ///
                      yind ydip yind_f ydip_f  ///
                      ores_f p_nrp yaohm yaohm_f thinc thinc_f 
merge 1:1 mergeid using "$wave_6_imputations_", keepusing(`income_w6_imput') nogen

***merge with wave 6 consumption data***
local income_w6_co co002e co003e co010_ co011e 
merge 1:1 mergeid using "$wave_6_co", keepusing(`income_w6_co') nogen


***create indicator of multiple couples in household
egen cplxhh = tag(hhid6 h6coupid)
egen multihouse = total(cplxhh) if inw6==1, by(hhid6)
recode multihouse (1=0) (2/10 = 1)

***Earnings*** 
// earnings from employment are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Earnings*** 
// earnings from employment are imputed at respondent level
*wave 6 respondent earnings from employment
gen r`wv'itearn = ydip
label variable r`wv'itearn  "r`wv'itearn:w`wv' Income: R Earnings from employment (after-tax)"

*wave 6 respondent flag for earnings from employment
imputation_flag ydip_f, gen(r`wv'itfearn)
label variable r`wv'itfearn "r`wv'itfearn:w`wv' IncFlag: R Earnings from employment (after-tax)"
label values r`wv'itfearn incflag

*wave 6 spouse earnings from employment
gen s`wv'itearn = .
spouse r`wv'itearn, result(s`wv'itearn) wave(`wv')
label variable s`wv'itearn  "s`wv'itearn:w`wv' Income: S Earnings from employment  (after-tax)"

*wave 6 spouse flag for earnings from employment
gen s`wv'itfearn = . 
spouse r`wv'itfearn, result(s`wv'itfearn) wave(`wv')
label variable s`wv'itfearn "s`wv'itfearn:w`wv' IncFlag: S Earnings from employment (after-tax)"
label values s`wv'itfearn incflag


***Capital Income***
// earnings from self-employment are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

// rental income is asked of the household respondent on behalf of 
// the whole household. The imputations for these questions are based
// the household respondent's answers and all members of the household
// are given the same value. Given this couple-level
// values can only be derived for couples without any additional household
// members.

// income from bank account and income from bonds, stocks, and mutal 
// funds are asked of the financial respondent on behalf of 
// the respondent and the spouse, if any spouse. The respondent and spouse
// can also choose to answer questions about their finanes separetly in which
// case both the respondent and the spouse answer asset questions. These values
// are imputed at the individual level and at the household level.
// Individual level imputations are available for all financial respondents. 
// All non financial respodents are assigned 0 values for the
// indvidual level asset imputation variables. 


**self-employment income
*wave 6 respondent earnings from self-employment
gen r`wv'itsemp = yind
label variable r`wv'itsemp "r`wv'itsemp:w`wv' Income: R Earnings from self-employment (after-tax)"

*wave 6 respondent flag for earnings from self-employment
imputation_flag yind_f, gen(r`wv'itfsemp)
label variable r`wv'itfsemp "r`wv'itfsemp:w`wv' IncFlag: R Earnings from self-employment (after-tax)"
label values r`wv'itfsemp incflag

*wave 6 spouse earnings from self-employment
gen s`wv'itsemp = .
spouse r`wv'itsemp, result(s`wv'itsemp) wave(`wv')
label variable s`wv'itsemp "s`wv'itsemp:w`wv' Income: S Earnings from self-employment (after-tax)"

*wave 6 spouse flag for earnings from self-employment
gen s`wv'itfsemp = . 
spouse r`wv'itfsemp, result(s`wv'itfsemp) wave(`wv')
label variable s`wv'itfsemp "s`wv'itfsemp:w`wv' IncFlag: S Earnings from self-employment (after-tax)"
label values s`wv'itfsemp incflag

*wave 6 couple-level earnings from self-employment
egen h`wv'itsemp = total(yind) if p_nrp == 0, by(h`wv'coupid)

imputation_flag yind_f, gen(h`wv'itfsemp) by(h`wv'coupid)
replace h`wv'itfsemp = -2 if p_nrp == 1

*wave 6 household-level earnings from self-employment
egen hh`wv'itsemp = total(yind) if p_nrp == 0, by(hh`wv'hhid)

imputation_flag yind_f, gen(hh`wv'itfsemp) by(hh`wv'hhid)
replace hh`wv'itfsemp = -2 if p_nrp == 1

**rental income***
*create more complete imputation flag for rental income
gen ysrent_hf = ysrent_f
replace ysrent_hf = 2 if ores_f == 2
replace ysrent_hf = 8 if ores_f == 8

*wave 6 couple-level rental income
gen h`wv'itrent = ysrent if multihouse == 0
label variable h`wv'itrent "h`wv'itrent:w`wv' Income: R+S Rental income (after-tax)"

imputation_flag ysrent_hf, gen(h`wv'itfrent) by(hh`wv'hhid)
replace h`wv'itfrent = -1 if multihouse == 1
label variable h`wv'itfrent "h`wv'itfrent:w`wv' Impflag: R+S Rental income (after-tax)"
label values h`wv'itfrent incflag

*wave 6 household-level rental income
gen hh`wv'itrent = ysrent
label variable hh`wv'itrent "hh`wv'itrent:w`wv' Income: HHold Rental income (after-tax)"

imputation_flag ysrent_hf, gen(hh`wv'itfrent) by(hh`wv'hhid)
label variable hh`wv'itfrent "hh`wv'itfrent:w`wv' Income: HHold Rental income (after-tax)"
label values hh`wv'itfrent incflag

drop ysrent_hf

**interest income from financial assets
*wave 6 couple-level income from bank account, bonds, stocks, and mutual funds
gen h`wv'ittrest = ybabsmf
label variable h`wv'ittrest "h`wv'ittrest:w`wv' Income: R+S Interest income from financial assets (after-tax)"

imputation_flag ybabsmf_f, gen(h`wv'itftrest) by(h`wv'coupid)
label variable h`wv'itftrest "h`wv'itftrest:w`wv' Impflag: R+S Interest income from financial assets (after-tax)"
label values h`wv'itftrest incflag

*wave 6 household-level income from bank account, bonds, stocks, and mutal funds
egen hh`wv'ittrest = total(ybabsmf * (cplxhh==1)), by(hh`wv'hhid) m
label variable hh`wv'ittrest "hh`wv'ittrest:w`wv' Income: HHold Interest income from financial assets (after-tax)"

imputation_flag ybabsmf_f, gen(hh`wv'itftrest) by(hh`wv'hhid)
label variable hh`wv'itftrest "hh`wv'itftrest:w`wv' Impflag: HHold Interest income from financial assets (after-tax)"
label values hh`wv'itftrest incflag

*wave 6 couple-level capital income
generate double h`wv'itcap  = h`wv'itsemp + h`wv'itrent + h`wv'ittrest
label variable h`wv'itcap  "h`wv'itcap:w`wv' Income: R+S Capital Income (after-tax)"

*wave 6 couple-level flag for capital income
h_imputation_flag h`wv'itfsemp h`wv'itfrent h`wv'itftrest, gen(h`wv'itfcap)
label variable h`wv'itfcap "h`wv'itfcap:w`wv' IncFlag: R+S Capital Inc (after-tax)"
label values h`wv'itfcap incflag

*wave 6 household-level capital income
generate double hh`wv'itcap = hh`wv'itsemp + hh`wv'itrent + hh`wv'ittrest
label variable hh`wv'itcap  "hh`wv'itcap:w`wv' Income: HHold Capital Income (after-tax)"

*wave 6 household-level flag for capital income
h_imputation_flag hh`wv'itfsemp hh`wv'itfrent hh`wv'itftrest, gen(hh`wv'itfcap)
label variable hh`wv'itfcap "hh`wv'itfcap:w`wv' IncFlag: HHold Capital Inc (after-tax)"
label values hh`wv'itfcap incflag

drop h`wv'itsemp hh`wv'itsemp
drop h`wv'itfsemp hh`wv'itfsemp

***Employer/Private Pension and Annuity***
// incomes from pensions are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

// incomes from other regular payments are asked in the employment 
// and pensions module to all respondents. The value is imputed 
// at the respondent level

***Employer/Private Pension and Annuity
*wave 6 respondent income from private and occupational pensions
gen r`wv'itpena = ypen2 + yreg1 + ylsp2 + ylsr1
label variable r`wv'itpena "r`wv'itpena:w`wv' Income: R Pension + Annuity (after-tax)"

*wave 6 respondent income flag from private and occupational pensions
imputation_flag ypen2_f yreg1_f ylsp2_f ylsr1_f, gen(r`wv'itfpena)
label variable r`wv'itfpena "r`wv'itfpena:w`wv' ImpFlag: R Pension + Annuity (after-tax)"
label values r`wv'itfpena incflag

*wave 6 spouse income from private and occupational pensions
gen s`wv'itpena = .
spouse r`wv'itpena, result(s`wv'itpena) wave(`wv')
label variable s`wv'itpena "s`wv'itpena:w`wv' Income: S Pension + Annuity (after-tax)"

*wave 6 spouse flag for income from private and occupational pensions
gen s`wv'itfpena = .
spouse r`wv'itfpena, result(s`wv'itfpena) wave(`wv')
label variable s`wv'itfpena "s`wv'itfpena:w`wv' ImpFlag: S Pension + Annuity (after-tax)"
label values s`wv'itfpena incflag

***Public Pensions***
// incomes from pensions are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Public Disability Pensions
*wave 6 respondent income from public disability pensions
gen r`wv'itssdi = ypen3 + ylsp3
label variable r`wv'itssdi "r`wv'itssdi:w`wv' Income: R Public Disability Pensions (after-tax)"

*wave 6 respondent flag for income from public disability pensions
imputation_flag ypen3_f ylsp3_f, gen(r`wv'itfssdi)
label variable r`wv'itfssdi "r`wv'itfssdi:w`wv' IncFlag: R Public Disability Pensions (after-tax)"
label values r`wv'itfssdi incflag

*wave 6 spouse income from public disability pensions
gen s`wv'itssdi = .
spouse r`wv'itssdi, result(s`wv'itssdi) wave(`wv')
label variable s`wv'itssdi "s`wv'itssdi:w`wv' Income: S Public Disability Pensions (after-tax)"

*wave 6 spouse flag for income from public disability pensions
gen s`wv'itfssdi = .
spouse r`wv'itfssdi, result(s`wv'itfssdi) wave(`wv')
label variable s`wv'itfssdi "s`wv'itfssdi:w`wv' IncFlag: S Public Disability Pensions (after-tax)"
label values s`wv'itfssdi incflag

***Public Old Age Pensions
*wave 6 respondent income from public old age + early retirement pensions
gen r`wv'itsret = ypen1 + ylsp1
label variable r`wv'itsret "r`wv'itsret:w`wv' Income: R Public Old Age + Early Retirement Pensions (after-tax)"

*wave 6 respondent flag for income from  public old age + early retirement pensions
imputation_flag ypen1_f ylsp1_f, gen(r`wv'itfsret)
label variable r`wv'itfsret "r`wv'itfsret:w`wv' IncFlag: R Public Old Age + Early Retirement Pensions (after-tax)"
label values r`wv'itfsret incflag

*wave 6 spouse income from public old age + early retirement pensions
gen s`wv'itsret = .
spouse r`wv'itsret, result(s`wv'itsret) wave(`wv')
label variable s`wv'itsret "s`wv'itsret:w`wv' Income: S Public Old Age + Early Retirement Pensions (after-tax)"

*wave 6 spouse flag for income from public old age + early retirement pensions
gen s`wv'itfsret = .
spouse r`wv'itfsret, result(s`wv'itfsret) wave(`wv')
label variable s`wv'itfsret "s`wv'itfsret:w`wv' IncFlag: S Public Old Age + Early Retirement Pensions (after-tax)"
label values s`wv'itfssdi incflag

***Total Public Pensions
*wave 6 respondent income from total public pensions
gen r`wv'itpubpen = r`wv'itssdi + r`wv'itsret
label variable r`wv'itpubpen "r`wv'itpubpen:w`wv' Income: R Public Pensions (after-tax)"

*wave 6 respondent flag for income from total public pensions
h_imputation_flag r`wv'itfssdi r`wv'itfsret, gen(r`wv'itfpubpen)
label variable r`wv'itfpubpen "r`wv'itfpubpen:w`wv' Impflag: R Public Pensions (after-tax)"
label values r`wv'itfpubpen incflag

*wave 6 spouse income from total public pensions
gen s`wv'itpubpen = .
spouse r`wv'itpubpen, result(s`wv'itpubpen) wave(`wv')
label variable s`wv'itpubpen "s`wv'itpubpen:w`wv' Income: S Public Pensions (after-tax)"

*wave 6 spouse flag for income from total public pensions
gen s`wv'itfpubpen = .
spouse r`wv'itfpubpen, result(s`wv'itfpubpen) wave(`wv')
label variable s`wv'itfpubpen "s`wv'itfpubpen:w`wv' IncFlag: S Public Pensions (after-tax)"
label values s`wv'itfpubpen incflag

***Other Government Transfers***
// incomes from other goverment transfers are asked in the employment and pensions module
// to all respondents. The value is imputed at the respondent level

***Other Government Transfers
*wave 6 respondent income from Other Government Transfers
gen r`wv'itgxfr = ypen4 + ypen5 + ylsp4 + ylsp5
label variable r`wv'itgxfr "r`wv'itgxfr:w`wv' Income: R Other Government Transfers (after-tax)"

*wave 6 respondent flag for income from Other Government Transfers
imputation_flag ypen4_f ypen5_f ylsp4_f ylsp5_f, gen(r`wv'itfgxfr)
label variable r`wv'itfgxfr "r`wv'itfgxfr:w`wv' IncFlag: R Other Government Transfers (after-tax)"
label values r`wv'itfgxfr incflag

*wave 6 spouse income from Other Government Transfers
gen s`wv'itgxfr = .
spouse r`wv'itgxfr, result(s`wv'itgxfr) wave(`wv')
label variable s`wv'itgxfr "s`wv'itgxfr:w`wv' Income: S Other Government Transfers (after-tax)"

*wave 6 spouse flag for income from Other Government Transfers
gen s`wv'itfgxfr = .
spouse r`wv'itfgxfr, result(s`wv'itfgxfr) wave(`wv')
label variable s`wv'itfgxfr "s`wv'itfgxfr:w`wv' IncFlag: S Other Government Transfers (after-tax)"
label values s`wv'itfgxfr incflag

***Other Income***
// incomes from other regular payments are asked in the employment 
// and pensions module to all respondents. The value is imputed 
// at the respondent level

*wave 6 respondent income from other income
gen r`wv'itothr =  yreg2 + ylsr2
label variable r`wv'itothr "r`wv'itothr:w`wv' Income: R Other Income (after-tax)"

*wave 6 respondent flag for income from other income
imputation_flag yreg2_f ylsr2_f, gen(r`wv'itfothr)
label variable r`wv'itfothr "r`wv'itfothr:w`wv' IncFlag: R Other Income (after-tax)"
label values r`wv'itfothr incflag

*wave 6 spouse income from other income
gen s`wv'itothr = .
spouse r`wv'itothr, result(s`wv'itothr) wave(`wv')
label variable s`wv'itothr "s`wv'itothr:w`wv' Income: S Other Income (after-tax)"

*wave 6 spouse flag for income from other income
gen s`wv'itfothr = .
spouse r`wv'itfothr, result(s`wv'itfothr) wave(`wv')
label variable s`wv'itfothr "s`wv'itfothr:w`wv' IncFlag: S Other Income (after-tax)"
label values s`wv'itfothr incflag

***Income from other household members***
// income from other household members is imputed at full-household level

****Income from other household members
**wave 6 household-level income from other household members
*gen hh`wv'itothhh = yaohm
*label variable hh`wv'itothhh "hh`wv'itothhh:w`wv' Income:HHold Other Household Members' Inc (after-tax)"
*
*imputation_flag yaohm_f, gen(hh`wv'itfothhh) by(hh`wv'hhid)
*label variable hh`wv'itfothhh "hh`wv'itfothhh:w`wv' Impflag:HHold Other Household Members' Inc (after-tax)"
*label values hh`wv'itfothhh incflag

***Total Income***

*couple-level earnings from employment
egen h`wv'itearn = total(r`wv'itearn) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfearn, gen(h`wv'itfearn) by(h`wv'coupid)
replace h`wv'itfearn = -2 if p_nrp == 1

*couple-level income from private pensions
egen h`wv'itpena = total(r`wv'itpena) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfpena, gen(h`wv'itfpena) by(h`wv'coupid)
replace h`wv'itfpena = -2 if p_nrp == 1

*couple-level income from public pensions
egen h`wv'itpubpen = total(r`wv'itpubpen) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfpubpen, gen(h`wv'itfpubpen) by(h`wv'coupid)
replace h`wv'itfpubpen = -2 if p_nrp == 1

*couple-level income from goverment transfers
egen h`wv'itgxfr = total(r`wv'itgxfr) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfgxfr, gen(h`wv'itfgxfr) by(h`wv'coupid)
replace h`wv'itfgxfr = -2 if p_nrp == 1

*couple-level income from other income
egen h`wv'itothr = total(r`wv'itothr) if p_nrp == 0, by(h`wv'coupid)

h_imputation_flag r`wv'itfothr, gen(h`wv'itfothr) by(h`wv'coupid)
replace h`wv'itfothr = -2 if p_nrp == 1

*household-level earnings from employment
egen hh`wv'itearn = total(r`wv'itearn) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfearn, gen(hh`wv'itfearn) by(hh`wv'hhid)
replace hh`wv'itfearn = -2 if p_nrp == 1

*household-level income from private pensions
egen hh`wv'itpena = total(r`wv'itpena) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfpena, gen(hh`wv'itfpena) by(hh`wv'hhid)
replace hh`wv'itfpena = -2 if p_nrp == 1

*household-level income from public pensions
egen hh`wv'itpubpen = total(r`wv'itpubpen) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfpubpen, gen(hh`wv'itfpubpen) by(hh`wv'hhid)
replace hh`wv'itfpubpen = -2 if p_nrp == 1

*household-level income from government transfers
egen hh`wv'itgxfr = total(r`wv'itgxfr) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfgxfr, gen(hh`wv'itfgxfr) by(hh`wv'hhid)
replace hh`wv'itfgxfr = -2 if p_nrp == 1

*household-level income from other income
egen hh`wv'itothr = total(r`wv'itothr) if p_nrp == 0, by(hh`wv'hhid)

h_imputation_flag r`wv'itfothr, gen(hh`wv'itfothr) by(hh`wv'hhid)
replace hh`wv'itfothr = -2 if p_nrp == 1

*household-level total income
gen hh`wv'ittotinc = thinc

imputation_flag thinc_f, gen(hh`wv'itftotinc) by(hh`wv'hhid)

*wave 6 couple-level total income
generate double h`wv'ittot = h`wv'itearn + h`wv'itcap + h`wv'itpena + h`wv'itpubpen + h`wv'itgxfr + h`wv'itothr
label variable h`wv'ittot "h`wv'ittot:w`wv' Incm: R+S Total Income (after-tax)"

*wave 6 couple-level flag for total income
h_imputation_flag h`wv'itfearn h`wv'itfcap h`wv'itfpena h`wv'itfpubpen h`wv'itfgxfr h`wv'itfothr, gen(h`wv'itftot)
label variable h`wv'itftot "h`wv'itftot:w`wv' IncFlag: R+S Total Inc (after-tax)"
label values h`wv'itftot incflag

**wave 6 household-level total income
*generate double hh`wv'ittot = hh`wv'ittotinc
*label variable hh`wv'ittot "hh`wv'ittot:w`wv' Incm:HHold Total Income (after-tax)"
*
**wave 6 household-level flag for total income
*h_imputation_flag hh`wv'itfearn hh`wv'itfcap hh`wv'itfpena hh`wv'itfpubpen hh`wv'itfgxfr hh`wv'itfothr hh`wv'itfothhh, gen(hh`wv'itftot)
*replace hh`wv'itftot = -2 if hh`wv'itftotinc == 9
*label variable hh`wv'itftot "hh`wv'itftot:w`wv' IncFlag:HHold Total Income (after-tax)"
*label values hh`wv'itftot incflag

drop h`wv'itearn  h`wv'itpena  h`wv'itpubpen  h`wv'itothr h`wv'itgxfr ///
		 h`wv'itfearn h`wv'itfpena h`wv'itfpubpen h`wv'itfothr h`wv'itfgxfr ///
		 hh`wv'itearn  hh`wv'itpena  hh`wv'itpubpen  hh`wv'itothr hh`wv'itgxfr  ///
		 hh`wv'itfearn hh`wv'itfpena hh`wv'itfpubpen hh`wv'itfothr hh`wv'itfgxfr ///
         hh`wv'ittotinc hh`wv'itftotinc
		 
drop cplxhh multihouse

***Consumption: food at home***
*wave 6 household consumption food at home
gen foodh = .
replace foodh = .m if (co011e > 2000000 | co011e < 0) & inw`wv' == 1
replace foodh = 0 if co010_ == 5
replace foodh = co011e if inrange(co011e,0,2000000) & co010_ == 1
gen foodi = .
replace foodi = .m if (co002e > 2000000 | co002e < 0) & inw`wv' == 1
replace foodi = co002e if inrange(co002e,0,2000000)
gen hh`wv'cfoodi1m = .
replace hh`wv'cfoodi1m = .m if (co002e == . | co011e == . | co010_ == . | foodh == .m | foodi == .m) & inw`wv' == 1
replace hh`wv'cfoodi1m = .r if co002e == -9999992 | co010_ == -9999992 | co011e == -9999992
replace hh`wv'cfoodi1m = .d if co002e == -9999991 | co010_ == -9999991 | co011e == -9999991
replace hh`wv'cfoodi1m = foodh + foodi if inrange(foodh,0,2000000) & inrange(foodi,0,2000000)
label variable hh`wv'cfoodi1m "hh`wv'cfoodi1m:w`wv' hhold monthly food consumption in house"

bysort h`wv'coupid: replace hh`wv'cfoodi1m = hh`wv'cfoodi1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfoodi1m = hh`wv'cfoodi1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

drop foodh foodi

***Consumption: food outside home***
*wave 6 household consumption food outside home
gen hh`wv'cfoodo1m = .
replace hh`wv'cfoodo1m = .m if (co003e == . | co003e > 2000000 | co003e < 0) & inw`wv' == 1
replace hh`wv'cfoodo1m = .r if co003e == -9999992
replace hh`wv'cfoodo1m = .d if co003e == -9999991
replace hh`wv'cfoodo1m = co003e if inrange(co003e,0,2000000)
label variable hh`wv'cfoodo1m "hh`wv'cfoodo1m:w`wv' hhold monthly food consumption outside house"

bysort h`wv'coupid: replace hh`wv'cfoodo1m = hh`wv'cfoodo1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfoodo1m = hh`wv'cfoodo1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse

***Consumption: total food***
*wave 6 household consumption total food
gen hh`wv'cfood1m = .
replace hh`wv'cfood1m = .m if hh`wv'cfoodi1m == .m | hh`wv'cfoodo1m == .m
replace hh`wv'cfood1m = .r if co002e == -9999992 | co003e == -9999992 | co011e == -9999992 | co010_ == -9999992
replace hh`wv'cfood1m = .d if co002e == -9999991 | co003e == -9999991 | co011e == -9999991 | co010_ == -9999991
replace hh`wv'cfood1m = hh`wv'cfoodi1m + hh`wv'cfoodo1m if inrange(hh`wv'cfoodi1m,0,4000000) & inrange(hh`wv'cfoodo1m,0,2000000)
label variable hh`wv'cfood1m "hh`wv'cfood1m:w`wv' hhold total monthly food consumption"

bysort h`wv'coupid: replace hh`wv'cfood1m = hh`wv'cfood1m[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'hhr == 0 // copy values to spouse
bysort h`wv'coupid: replace hh`wv'cfood1m = hh`wv'cfood1m[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'hhr == 0 // copy values to spouse


***drop SHARE wave 6 financial derived file raw variables***
drop `income_w6_imput'

***drop wave 6 consumption file raw variables***
drop `income_w6_co'

******************************************************************************************


*Hospital utilization/insurance
label define diff ///
   0 "0.No"  ///
   1 "1.Yes" ///
   .m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .q ".q:Not asked" ///
   .g ".g:Covered by gov ins" ///
   .n ".n:In NHM"  ///
   .v ".v:Sp Nr" 

*insurance
label define ins ///
   0 "0.No"  ///
   1 "1.Yes" ///
   .m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .q ".q:Not asked" ///
   .g ".g:Covered by gov ins" ///
   .n ".n:In NHM"  ///
   .v ".v:Sp Nr" 



 
******************************************************************************************

	

*set wave number
local wv = 1

***merge with wave 1 ins data***
local ins_w1_hc  hc057_ country hc059d1 hc059d2 hc059d3 hc059d4 hc059d5 hc059d6 ///
  								hc059d7 hc059d8 hc059d9 hc059d10 hc059d11 hc059d12 hc059dot ///
   								hc010_ hc012_ hc013_ hc014_ hc029_ hc030_ hc031_ hc002_ ///
                  hc032d1 hc032d2 hc032d3 hc032dno hc038_ hc045e hc047e hc049e hc051e ///
                  language
merge 1:1 mergeid using "$wave_1_hc", keepusing(`ins_w1_hc')  nogen

***merge with wave 1 asset data***
local ins_w1_as as002d7 
merge 1:1 mergeid using "$wave_1_as", keepusing(`ins_w1_as') nogen

***merge with wave 1 physical health data***
local ins_w1_ph  ph011d1 ph011d2 ph011d3 ph011d4 ph011d5 ph011d6 ph011d7 ph011d8 /// 
									ph011d9 ph011d10 ph011d11 ph011d12 ph011d13 ph011d14 ph011dot
merge 1:1 mergeid using "$wave_1_ph", keepusing(`ins_w1_ph') nogen


***Whether Any Hospital Stay***
*wave 1 respondent any hospital stay
gen r1hosp1y=.
missing_common hc012_,result(r1hosp1y)
replace r1hosp1y=1 if hc012_==1
replace r1hosp1y=0 if hc012_==5
label variable r1hosp1y "r1hosp1y:w1 R Hospital stay, pr 12 mos"
label values r1hosp1y diff

*wave 1 spouse any hospital stay
gen s1hosp1y=.
spouse r1hosp1y, result(s1hosp1y) wave(1)
label variable s1hosp1y "s1hosp1y:w1 S Hospital stay, pr 12 mos"
label values s1hosp1y diff

***Number of Hospital Stays***
*wave 1 respondent # hospital stays
gen r1hsptim1y=.
missing_common hc013_,result(r1hsptim1y)
replace r1hsptim1y=hc013_ if inrange(hc013_,0,10)
replace r1hsptim1y=0 if hc012==5
label variable r1hsptim1y "r1hsptim1y:w1 R # Hospital stays, pr 12 mos"

*wave 1 spouse # hospital stays
gen s1hsptim1y=.
spouse r1hsptim1y, result(s1hsptim1y) wave(1)
label variable s1hsptim1y "s1hsptim1y:w1 S # Hospital stays, pr 12 mos"

***Number Nights in Hospital***
*wave 1 respondent # nights in hospital 
gen r1hspnit1y=.
missing_common hc014_,result(r1hspnit1y)
replace r1hspnit1y=hc014_ if inrange(hc014_,0,365)
replace r1hspnit1y=0 if hc012==5
label variable r1hspnit1y "r1hspnit1y:w1 R # Nights in hosp, pr 12 mos"

*wave 1 spouse # nights in hospital 
gen s1hspnit1y=.
spouse r1hspnit1y, result(s1hspnit1y) wave(1)
label variable s1hspnit1y "s1hspnit1y:w1 S # Nights in hosp, pr 12 mos"

***Whether Any Nursing Home Stay***
*wave 1 respondent Nursing home stay
gen r1nrshom1y=.
missing_common hc029_,result(r1nrshom1y)
replace r1nrshom1y=1 if inlist(hc029_,1,3)
replace r1nrshom1y=0 if hc029_==5
label variable r1nrshom1y "r1nrshom1y:w1 R Nurs home stay, pr 12 mos"
label values r1nrshom1y diff

*wave 1 spouse Nursing home stay
gen s1nrshom1y=.
spouse r1nrshom1y, result(s1nrshom1y) wave(1)
label variable s1nrshom1y "s1nrshom1y:w1 S Nurs home stay, pr 12 mos"
label values s1nrshom1y diff

***Number Nursing Home Stays***
*wave 1 respondent # Nursing home stays
gen r1nrstim1y=.
missing_common hc030_,result(r1nrstim1y)
replace r1nrstim1y=hc030_ if inrange(hc030_,1,365)
replace r1nrstim1y=.n if hc029_==3
replace r1nrstim1y=0 if hc029_==5
label variable r1nrstim1y "r1nrstim1y:w1 R # Nurs home stays, pr 12 mos"

*wave 1 spouse # Nursing home stays
gen s1nrstim1y=.
spouse r1nrstim1y, result(s1nrstim1y) wave(1)
label variable s1nrstim1y "s1nrstim1y:w1 S # Nurs home stays, pr 12 mos"

***Number Nights in Nursing Home***
*wave 1 respondent # Nights in nurs home
gen r1nrsnit1y=.
missing_common hc031_,result(r1nrsnit1y)
replace r1nrsnit1y=hc031_*7 if inrange(hc031_,1,52)
replace r1nrsnit1y=365 if hc029_==3
replace r1nrsnit1y=0 if hc029_==5
label variable r1nrsnit1y "r1nrsnit1y:w1 R # Nights in Nurs home, pr 12 mos"

*wave 1 spouse # Nights in nurs home
gen s1nrsnit1y=.
spouse r1nrsnit1y, result(s1nrsnit1y) wave(1)
label variable s1nrsnit1y "s1nrsnit1y:w1 S # Nights in Nurs home, pr 12 mos"

***Whether Live in Nursing Home***
*wave 1 respondent Live in Nursing home 
gen r1nhmliv1y=.
missing_common hc029_,result(r1nhmliv1y)
replace r1nhmliv1y=1 if inlist(hc029_,3)
replace r1nhmliv1y=0 if inlist(hc029_,1,5)
label variable r1nhmliv1y "r1nhmliv1y:w1 R Live in Nurs home, pr 12 mos"
label values r1nhmliv1y diff

*wave 1 spouse Live in Nursing home 
gen s1nhmliv1y=.
spouse r1nhmliv1y, result(s1nhmliv1y) wave(1)
label variable s1nhmliv1y "s1nhmliv1y:w1 S R Live in Nurs home, pr 12 mos"
label values s1nhmliv1y diff

***Whether Any Doctor Visit***
*wave 1 respondent any doctor1y visit
gen r1doctor1y=.
missing_common hc002_,result(r1doctor1y)
replace r1doctor1y=1 if inrange(hc002_,1,98)
replace r1doctor1y=0 if hc002_==0
label variable r1doctor1y "r1doctor1y:w1 R doctor1y visit, pr 12 mos"
label values r1doctor1y diff

*wave 1 spouse any doctor1y visit
gen s1doctor1y=.
spouse r1doctor1y, result(s1doctor1y) wave(1)
label variable s1doctor1y "s1doctor1y:w1 S doctor1y visit, pr 12 mos"
label values s1doctor1y diff

***Number of Doctor Visits***
*wave 1 respondent # of times doctor1y visit
gen r1doctim1y=.
missing_common hc002_,result(r1doctim1y)
replace r1doctim1y=hc002_ if inrange(hc002_,0,98)
label variable r1doctim1y "r1doctim1y:w1 R # doctor1y visits, pr 12 mos"

*wave 1 spouse # of times doctor1y visit
gen s1doctim1y=.
spouse r1doctim1y, result(s1doctim1y) wave(1)
label variable s1doctim1y "s1doctim1y:w1 S # doctor1y visits, pr 12 mos"

***Whether Any Home Health Care***
*wave 1 respondent home hlth care
gen r1homcar1y=.
replace r1homcar1y = .m if (missing(hc032d1) | missing(hc032d2) | missing(hc032d3) | missing(hc032dno)) & inw1 == 1
replace r1homcar1y=.d  if hc032d1==-1 & hc032d2==-1 & hc032d3==-1
replace r1homcar1y=.r  if hc032d1==-2 & hc032d2==-2 & hc032d3==-2
replace r1homcar1y=.n if hc029==3
replace r1homcar1y=1 if hc032d1==1 | hc032d2==1 | hc032d3==1
replace r1homcar1y=0 if hc032dno==1
label variable r1homcar1y "r1homcar1y:w1 R Home hlth care, pr 12 mos"
label values r1homcar1y diff

*wave 1 spouse home hlth care
gen s1homcar1y=.
spouse r1homcar1y, result(s1homcar1y) wave(1)
label variable s1homcar1y "s1homcar1y:w1 S Home hlth care, pr 12 mos"
label values s1homcar1y diff

***Whether Received Care from Private Provider***
*wave 1 respondent receive care from private provider
gen r1pricar1y=.
missing_common hc038_,result(r1pricar1y)
replace r1pricar1y=.q if inlist(language,23,24,14)
replace r1pricar1y=1 if hc038_==1
replace r1pricar1y=0 if hc038_==5
label variable r1pricar1y "r1pricar1y:w1 R receive care from private provider, pr 12 mos"
label values r1pricar1y diff

*wave 1 spouse receive care from private provider
gen s1pricar1y=.
spouse r1pricar1y, result(s1pricar1y) wave(1)
label variable s1pricar1y "s1pricar1y:w1 S receive care from private provider, pr 12 mos"
label values s1pricar1y diff

***Whether Taking Any Drugs***
*wave 1 respondent takes any drugs
gen r1drugs1w = .
missing_common ph011d1 ph011d2 ph011d3 ph011d4 ph011d5 ph011d6 ph011d7 ph011d8 ph011d9 ///
		 ph011d10 ph011d11 ph011d12 ph011d13 ph011d14 ph011dot, result(r1drugs1w)
replace r1drugs1w = 0 if ph011d1==0|ph011d2==0|ph011d3==0|ph011d4==0|ph011d5==0|ph011d6==0|ph011d7==0| ///
		ph011d8==0|ph011d9==0|ph011d10==0|ph011d11==0|ph011d12==0|ph011d13==0|ph011d14==0|ph011dot==0
replace r1drugs1w = 1 if ph011d1==1|ph011d2==1|ph011d3==1|ph011d4==1|ph011d5==1|ph011d6==1|ph011d7==1| ///
		ph011d8==1|ph011d9==1|ph011d10==1|ph011d11==1|ph011d12==1|ph011d13==1|ph011d14==1|ph011dot==1
label variable r1drugs1w "r1drugs1w:w1 R reg take rx, prv week"
label values r1drugs1w diff

*wave 1 spouse takes any drugs
gen s1drugs1w = .
spouse r1drugs1w, result(s1drugs1w) wave(1)
label variable s1drugs1w "s1drugs1w:w1 S reg take rx, prv week"
label values s1drugs1w diff

***Whether Had Dental Visit***
*wave 1 respondent had dental visit
gen r1dentst1y = .
missing_common hc010_, result(r1dentst1y)
replace r1dentst1y = 0 if hc010_ == 5
replace r1dentst1y = 1 if hc010_ == 1
label variable r1dentst1y "r1dentst1y:w1 R dental visit, prv yr"
label values r1dentst1y diff

*wave 1 spouse had dental visit
gen s1dentst1y = .
spouse r1dentst1y, result(s1dentst1y) wave(1)
label variable s1dentst1y "s1dentst1y:w1 S dental visit, prv yr"
label values s1dentst1y diff

***Out of Pocket Hospital Expenditures***
*wave 1 respondent oop hospital expenditures
gen r1oophos1y = .
replace r1oophos1y = .m if hc045e == . & inw`wv' == 1
replace r1oophos1y = .d if hc045e == -9999991
replace r1oophos1y = .r if hc045e == -9999992
replace r1oophos1y = 0 if hc012_ == 5
replace r1oophos1y = hc045e if inrange(hc045e,0,30000)
label variable r1oophos1y "r1oophos1y:w1 R hospitalization oop expenditure, prv yr"

*wave 1 spouse oop hospital expenditures
gen s1oophos1y = .
spouse r1oophos1y, result(s1oophos1y) wave(1)
label variable s1oophos1y "s1oophos1y:w1 S hospitalization oop expenditure, prv yr"

***Out of Pocket Outpatient Care Expenditures***
*wave 1 respondent oop outpatient care expenditures
gen r1oopdoc1y = .
replace r1oopdoc1y = .m if hc047e == . & inw`wv' == 1
replace r1oopdoc1y = .d if hc047e == -9999991
replace r1oopdoc1y = .r if hc047e == -9999992
replace r1oopdoc1y = hc047e if inrange(hc047e,0,100000)
replace r1oopdoc1y = .m if inrange(hc047e,5000000,6000000)
label variable r1oopdoc1y "r1oopdoc1y:w1 R doctor oop expenditure, prv yr"

*wave 1 spouse oop outpatient care expenditures
gen s1oopdoc1y = .
spouse r1oopdoc1y, result(s1oopdoc1y) wave(1)
label variable s1oopdoc1y "s1oopdoc1y:w1 S doctor oop expenditure, prv yr"

***Out of Pocket Drug Expenditures***Not in Codebook, Part of Sum
*wave 1 respondent oop drug expenditures
gen r1oopdrug1y = .
replace r1oopdrug1y = .m if hc049e == . & inw`wv' == 1
replace r1oopdrug1y = .d if hc049e == -9999991
replace r1oopdrug1y = .r if hc049e == -9999992
replace r1oopdrug1y = hc049e if inrange(hc049e,0,1200000)
label variable r1oopdrug1y "r1oopdrug1y:w1 R drug oop expenditure, prv yr"

*wave 1 spouse oop drug expenditures
gen s1oopdrug1y = .
spouse r1oopdrug1y, result(s1oopdrug1y) wave(1)
label variable s1oopdrug1y "s1oopdrug1y:w1 S drug oop expenditure, prv yr"

***Out of Pocket Day Care/Nursing Home/Home Care Expenditures***Not in Codebook, Part of Sum
*wave 1 respondent oop day care/nursing home/home care expenditures
gen r1oophmcr1y = .
replace r1oophmcr1y = .m if hc051e == . & inw`wv' == 1
replace r1oophmcr1y = .d if hc051e == -9999991
replace r1oophmcr1y = .r if hc051e == -9999992
replace r1oophmcr1y = hc051e if inrange(hc051e,0,50000)
replace r1oophmcr1y = 0 if hc029_ == 5 | hc032dno == 1
label variable r1oophmcr1y "r1oophmcr1y:w1 R home care/nursing home oop expenditure, prv yr"

*wave 1 spouse oop day care/nursing home/home care expenditures
gen s1oophmcr1y = .
spouse r1oophmcr1y, result(s1oophmcr1y) wave(1)
label variable s1oophmcr1y "s1oophmcr1y:w1 S home care/nursing home oop expenditure, prv yr"

***Total Out of Pocket Medical Expenditures***
*wave 1 respondent total oop medical expenditures
egen r1oopmd1y = rowtotal(r1oophos1y r1oopdoc1y r1oopdrug1y r1oophmcr1y),m
replace r1oopmd1y = .m if r1oopmd1y == . & inw`wv' == 1
replace r1oopmd1y = .d if r1oopmd1y == .m & r1oophos1y==.d & r1oopdoc1y==.d & r1oopdrug1y==.d & r1oophmcr1y==.d
replace r1oopmd1y = .r if r1oopmd1y == .m & r1oophos1y==.r & r1oopdoc1y==.r & r1oopdrug1y==.r & r1oophmcr1y==.r
label variable r1oopmd1y "r1oopmd1y:w1 R oop medical expenditures, prv yr"

*wave 1 spouse total oop medical expenditures
gen s1oopmd1y = .
spouse r1oopmd1y, result(s1oopmd1y) wave(1)
label variable s1oopmd1y "s1oopmd1y:w1 S oop medical expenditures, prv yr"

***Whether Covered by Government Health Insurance Program ***
*wave 1 respondent cover by government Health insurance program
gen r1higov=.
missing_common hc057_,result(r1higov)
replace r1higov=1 if hc057_ == 1
replace r1higov=0 if hc057_ ==5
replace r1higov=.q if inlist(country,13,15,16,18,19,20, 25)
label variable r1higov "r1higov:w1 R cover by Gov Health Ins"
label values r1higov ins

*wave 1 spouse cover by government Health insurance program
gen s1higov=.
spouse r1higov, result(s1higov) wave(1)
label variable s1higov "s1higov:w1 S cover by Gov Health Ins"
label values s1higov ins

***Whether Any Voluntary Health Insurance***
*wave 1 respondent cover by any Voluntary Health insurance 
gen r1hiothp=.
replace r1hiothp = .m if (missing(hc059d1) | missing(hc059d2) | missing(hc059d3) | missing(hc059d4) ///
	| missing(hc059d5) | missing(hc059d6) | missing(hc059d7) | missing(hc059d8) | missing(hc059d9) ///
	| missing(hc059d10) | missing(hc059d11) | missing(hc059d12) | missing(hc059dot)) & inw`wv' == 1
replace r1hiothp=0 if hc059d1==0|hc059d2==0|hc059d3==0|hc059d4==0|hc059d5==0|hc059d6==0 ///
  |hc059d7==0|hc059d8==0|hc059d9==0|hc059d10==0|hc059d11==0|hc059d12==0|hc059dot==0
replace r1hiothp=.d if hc059d1==-1
replace r1hiothp=.r if hc059d1==-2
replace r1hiothp=1 if hc059d1==1|hc059d2==1|hc059d3==1|hc059d4==1|hc059d5==1|hc059d6==1 ///
  |hc059d7==1|hc059d8==1|hc059d9==1|hc059d10==1|hc059d11==1|hc059d12==1|hc059dot==1
replace r1hiothp=.q if inlist(country,13,15,16,18,19,20, 25)
replace r1hiothp=.g if inlist(r1higov,1)
label variable r1hiothp "r1hiothp:w1 R cover by Voluntary Health Ins"
label values r1hiothp ins

*wave 1 spouse cover by Voluntary Health insurance 
gen s1hiothp=.
spouse r1hiothp, result(s1hiothp) wave(1)
label variable s1hiothp "s1hiothp:w1 S cover by Voluntary Health Ins"
label values s1hiothp ins

***Whether Covered by Dental Insurance***
*wave 1 respondent cover by Dental insurance 
gen r1hident=.
replace r1hident = .m if (missing(hc059d5) | missing(hc059d1)) & inw`wv' == 1
replace r1hident=0 if hc059d5==0
replace r1hident=.d if hc059d1==-1
replace r1hident=.r if hc059d1==-2
replace r1hident=1 if hc059d5==1
replace r1hident=.q if inlist(country,13,15,16,18,19,20, 25)
replace r1hident=.g if inlist(r1higov,1)
label variable r1hident "r1hident:w1 R cover by Dental Ins"
label values r1hident ins

*wave 1 spouse cover by Dental insurance 
gen s1hident=.
spouse r1hident, result(s1hident) wave(1)
label variable s1hident "s1hident:w1 S cover by Dental Ins"
label values s1hident ins

***Whether Covered by Drug Insurance***
*wave 1 respondent cover by drug insurance 
gen r1hidrug=.
replace r1hidrug = .m if (missing(hc059d6) | missing(hc059d7)) & inw`wv' == 1
replace r1hidrug=0 if hc059d6==0|hc059d7==0
replace r1hidrug=.d if hc059d1==-1
replace r1hidrug=.r if hc059d1==-2
replace r1hidrug=1 if hc059d6==1|hc059d7==1
replace r1hidrug=.q if inlist(country,13,15,16,18,19,20, 25)
replace r1hidrug=.g if inlist(r1higov,1)
label variable r1hidrug "r1hidrug:w1 R cover by Drug expenses"
label values r1hidrug ins

*wave 1 spouse cover by drug insurance 
gen s1hidrug=.
spouse r1hidrug, result(s1hidrug) wave(1)
label variable s1hidrug "s1hidrug:w1 S cover by Drug Expenses"
label values s1hidrug ins

***Whether Covered by Long Term Care Insurance***
*wave 1 respondent cover by long term care insurance
gen r1hiltc=.
replace r1hiltc = .m if (missing(hc059d10) | missing(hc059d1)) & inw`wv' == 1
replace r1hiltc=0 if hc059d10==0
replace r1hiltc=.d if hc059d1==-1
replace r1hiltc=.r if hc059d1==-2
replace r1hiltc=1 if hc059d10==1
replace r1hiltc=.q if inlist(country,13,15,16,18,19,20, 25)
replace r1hiltc=.g if inlist(r1higov,1)
label variable r1hiltc "r1hiltc:w1 R cover by Long Term care Ins"
label values r1hiltc ins

*wave 1 spouse cover by long term care insurance
gen s1hiltc=.
spouse r1hiltc, result(s1hiltc) wave(1)
label variable s1hiltc "s1hiltc:w1 S cover by Long Term care Ins"
label values s1hiltc ins

***Whether Covered by Life Insurance
*wave 1 HH cover by life insurance
egen h1lifein=max(as002d7) if !mi(hhid1), by(hhid1)
replace h1lifein = .m if missing(h1lifein) & inw`wv' == 1
replace h1lifein=as002d7 if as002d7==0 & h1lifein==1
replace h1lifein=as002d7 if as002d7==1 & h1lifein==0
replace h1lifein=.d if h1lifein==-1
replace h1lifein=.r if h1lifein==-2
label variable h1lifein "h1lifein:w1 cover by life insurance"
label values h1lifein ins


***drop wave 1 ins file raw variables
drop `ins_w1_hc'

***drop wave 1 asset file raw variables
drop `ins_w1_as'

***drop wave 1 physical health file raw variables
drop `ins_w1_ph'

******************************************************************************************

*set wave number
local wv = 2

***merge with wave 2 Health utlization data***
local ins_w2_hc country hc010_ hc012_ hc013_ hc014_ hc029_ hc030_ hc031_ hc002_ ///
                   hc032d1 hc032d2 hc032d3 hc032dno hc038_ ///
                   hc045e hc047e hc049e hc051e language
merge 1:1 mergeid using "$wave_2_hc", keepusing(`ins_w2_hc') nogen

***merge with wave 2 asset data***
local ins_w2_as as067_  
merge 1:1 mergeid using "$wave_2_as", keepusing(`ins_w2_as') nogen

***merge with wave 2 physical health data***
local ins_w2_ph  ph011d1 ph011d2 ph011d3 ph011d4 ph011d5 ph011d6 ph011d7 ph011d8 /// 
									ph011d9 ph011d10 ph011d11 ph011d12 ph011d13 ph011d14 ph011dot 
merge 1:1 mergeid using "$wave_2_ph", keepusing(`ins_w2_ph') nogen

***merge with wave 2 technical variables data***
local ins_w2_techvar  mn024_
merge 1:1 mergeid using "$wave_2_techvar", keepusing(`ins_w2_techvar') nogen


***Whether Any Hospital Stay***
*wave 2 respondent any hospital stay
gen r2hosp1y=.
missing_common hc012_,result(r2hosp1y)
replace r2hosp1y=1 if hc012_==1
replace r2hosp1y=0 if hc012_==5
label variable r2hosp1y "r2hosp1y:w2 R Hospital stay, pr 12 mos"
label values r2hosp1y diff

*wave 2 spouse any hospital stay
gen s2hosp1y=.
spouse r2hosp1y, result(s2hosp1y) wave(2)
label variable s2hosp1y "s2hosp1y:w2 S Hospital stay, pr 12 mos"
label values s2hosp1y diff

***Number Hospital Stays***
*wave 2 respondent # hospital stays
gen r2hsptim1y=.
missing_common hc013_,result(r2hsptim1y)
replace r2hsptim1y=hc013_ if inrange(hc013_,0,10)
replace r2hsptim1y=0 if hc012==5
label variable r2hsptim1y "r2hsptim1y:w2 R # Hospital stays, pr 12 mos"

*wave 2 spouse # hospital stays
gen s2hsptim1y=.
spouse r2hsptim1y, result(s2hsptim1y) wave(2)
label variable s2hsptim1y "s2hsptim1y:w2 S # Hospital stays, pr 12 mos"

***Number Nights in Hospital***
*wave 2 respondent # nights in hospital 
gen r2hspnit1y=.
missing_common hc014_,result(r2hspnit1y)
replace r2hspnit1y=hc014_ if inrange(hc014_,0,365)
replace r2hspnit1y=0 if hc012==5
label variable r2hspnit1y "r2hspnit1y:w2 R # Nights in hosp, pr 12 mos"

*wave 2 spouse # nights in hospital 
gen s2hspnit1y=.
spouse r2hspnit1y, result(s2hspnit1y) wave(2)
label variable s2hspnit1y "s2hspnit1y:w2 S # Nights in hosp, pr 12 mos"

***Whether Any Nursing Home Stay***
*wave 2 respondent Nursing home stay
gen r2nrshom1y=.
missing_common hc029_,result(r2nrshom1y)
replace r2nrshom1y=.m if r2nrshom1y==. & inw`wv' == 1
replace r2nrshom1y=1 if inlist(hc029_,1,3)
replace r2nrshom1y=0 if hc029_==5
label variable r2nrshom1y "r2nrshom1y:w2 R Nurs home stay, pr 12 mos"
label values r2nrshom1y diff

*wave 2 spouse Nursing home stay
gen s2nrshom1y=.
spouse r2nrshom1y, result(s2nrshom1y) wave(2)
label variable s2nrshom1y "s2nrshom1y:w2 S Nurs home stay, pr 12 mos"
label values s2nrshom1y diff

***Number Nursing Home Stays***
*wave 2 respondent # Nursing home stays
gen r2nrstim1y=.
missing_common hc030_,result(r2nrstim1y)
replace r2nrstim1y=hc030_ if inrange(hc030_,1,365)
replace r2nrstim1y=.n if hc029_==3
replace r2nrstim1y=0 if hc029_==5
label variable r2nrstim1y "r2nrstim1y:w2 R # Nurs home stays, pr 12 mos"

*wave 2 spouse # Nursing home stays
gen s2nrstim1y=.
spouse r2nrstim1y, result(s2nrstim1y) wave(2)
label variable s2nrstim1y "s2nrstim1y:w2 S # Nurs home stays, pr 12 mos"

***Number Nights in Nursing Home***
*wave 2 respondent # Nights in nurs home
gen r2nrsnit1y=.
missing_common hc031_,result(r2nrsnit1y)
replace r2nrsnit1y=.m if hc031==999
replace r2nrsnit1y=hc031_*7 if inrange(hc031_,1,52)
replace r2nrsnit1y=365 if hc029_==3
replace r2nrsnit1y=0 if hc029_==5
label variable r2nrsnit1y "r2nrsnit1y:w2 R # Nights in Nurs home, pr 12 mos"

*wave 2 spouse # Nights in nurs home
gen s2nrsnit1y=.
spouse r2nrsnit1y, result(s2nrsnit1y) wave(2)
label variable s2nrsnit1y "s2nrsnit1y:w2 S # Nights in Nurs home, pr 12 mos"

***Whether Lives in Nursing Home***
*wave 2 respondent Live in Nursing home 
gen r2nhmliv1y=.
missing_common hc029_,result(r2nhmliv1y)
replace r2nhmliv1y=1 if inlist(hc029_,3)
replace r2nhmliv1y=0 if inlist(hc029_,1,5)
label variable r2nhmliv1y "r2nhmliv1y:w2 R Live in Nurs home, pr 12 mos"
label values r2nhmliv1y diff

*wave 2 spouse Live in Nursing home 
gen s2nhmliv1y=.
spouse r2nhmliv1y, result(s2nhmliv1y) wave(2)
label variable s2nhmliv1y "s2nhmliv1y:w2 S R Live in Nurs home, pr 12 mos"
label values s2nhmliv1y diff

***Whether Had Any Doctor Visit***
*wave 2 respondent any doctor1y visit
gen r2doctor1y=.
missing_common hc002_,result(r2doctor1y)
replace r2doctor1y=1 if inrange(hc002_,1,98)
replace r2doctor1y=0 if hc002_==0
label variable r2doctor1y "r2doctor1y:w2 R doctor1y visit, pr 12 mos"
label values r2doctor1y diff

*wave 2 spouse any doctor1y visit
gen s2doctor1y=.
spouse r2doctor1y, result(s2doctor1y) wave(2)
label variable s2doctor1y "s2doctor1y:w2 S doctor1y visit, pr 12 mos"
label values s2doctor1y diff

***Number of Doctor Visits***
*wave 2 respondent # of times doctor1y visit
gen r2doctim1y=.
missing_common hc002_,result(r2doctim1y)
replace r2doctim1y=hc002_ if inrange(hc002_,0,98)
label variable r2doctim1y "r2doctim1y:w2 R # doctor1y visits, pr 12 mos"

*wave 2 spouse # of times doctor1y visit
gen s2doctim1y=.
spouse r2doctim1y, result(s2doctim1y) wave(2)
label variable s2doctim1y "s2doctim1y:w2 S # doctor1y visits, pr 12 mos"

***Whether Had Any Home Health Care***
*wave 2 respondent home hlth care
gen r2homcar1y=.
replace r2homcar1y = .m if (missing(hc032d1) | missing(hc032d2) | missing(hc032d3) | missing(hc032dno)) & inw2==1 
replace r2homcar1y=.d  if hc032d1==-1 & hc032d2==-1 & hc032d3==-1
replace r2homcar1y=.r  if hc032d1==-2 & hc032d2==-2 & hc032d3==-2
replace r2homcar1y=.n if hc029==3 | mn024_==2
replace r2homcar1y=1 if hc032d1==1 | hc032d2==1 | hc032d3==1
replace r2homcar1y=0 if hc032dno==1 
label variable r2homcar1y "r2homcar1y:w2 R Home hlth care, pr 12 mos"
label values r2homcar1y diff

*wave 2 spouse home hlth care
gen s2homcar1y=.
spouse r2homcar1y, result(s2homcar1y) wave(2)
label variable s2homcar1y "s2homcar1y:w2 S Home hlth care, pr 12 mos"
label values s2homcar1y diff

***Whether Receive Any Care from Private Provider***
*wave 2 respondent receive care from private provider
gen r2pricar1y=.
missing_common hc038_,result(r2pricar1y)
replace r2pricar1y=.q if inlist(language,12,23)
replace r2pricar1y=1 if hc038_==1
replace r2pricar1y=0 if hc038_==5
label variable r2pricar1y "r2pricar1y:w2 R receive care from private provider, pr 12 mos"
label values r2pricar1y diff

*wave 2 spouse receive care from private provider
gen s2pricar1y=.
spouse r2pricar1y, result(s2pricar1y) wave(2)
label variable s2pricar1y "s2pricar1y:w2 S receive care from private provider, pr 12 mos"
label values s2pricar1y diff

***Whether Taking Any Drugs***
*wave 2 respondent takes any drugs
gen r2drugs1w = .
missing_common ph011d1 ph011d2 ph011d3 ph011d4 ph011d5 ph011d6 ph011d7 ph011d8 ph011d9 ///
		 ph011d10 ph011d11 ph011d12 ph011d13 ph011d14 ph011dot, result(r2drugs1w)
replace r2drugs1w = 0 if ph011d1==0|ph011d2==0|ph011d3==0|ph011d4==0|ph011d5==0|ph011d6==0|ph011d7==0| ///
		ph011d8==0|ph011d9==0|ph011d10==0|ph011d11==0|ph011d12==0|ph011d13==0|ph011d14==0|ph011dot==0
replace r2drugs1w = 1 if ph011d1==1|ph011d2==1|ph011d3==1|ph011d4==1|ph011d5==1|ph011d6==1|ph011d7==1| ///
		ph011d8==1|ph011d9==1|ph011d10==1|ph011d11==1|ph011d12==1|ph011d13==1|ph011d14==1|ph011dot==1
label variable r2drugs1w "r2drugs1w:w2 R reg take rx, prv week"
label values r2drugs1w diff

*wave 2 spouse takes any drugs
gen s2drugs1w = .
spouse r2drugs1w, result(s2drugs1w) wave(2)
label variable s2drugs1w "s2drugs1w:w2 S reg take rx, prv week"
label values s2drugs1w diff

***Whether Had Dental Visit***
*wave 2 respondent had dental visit
gen r2dentst1y = .
missing_common hc010_, result(r2dentst1y)
replace r2dentst1y = 0 if hc010_ == 5
replace r2dentst1y = 1 if hc010_ == 1
label variable r2dentst1y "r2dentst1y:w2 R dental visit, prv yr"
label values r2dentst1y diff

*wave 2 spouse had dental visit
gen s2dentst1y = .
spouse r2dentst1y, result(s2dentst1y) wave(2)
label variable s2dentst1y "s2dentst1y:w2 S dental visit, prv yr"
label values s2dentst1y diff

***Out of Pocket Hospital Expenditures***
*wave 2 respondent oop hospital expenditures
gen r2oophos1y = .
replace r2oophos1y = .m if hc045e == . & inw`wv' == 1
replace r2oophos1y = .d if hc045e == -9999991
replace r2oophos1y = .r if hc045e == -9999992
replace r2oophos1y = 0 if hc012_ == 5
replace r2oophos1y = hc045e if inrange(hc045e,0,40000)
label variable r2oophos1y "r2oophos1y:w2 R hospitalization oop expenditure, prv yr"

*wave 2 spouse oop hospital expenditures
gen s2oophos1y = .
spouse r2oophos1y, result(s2oophos1y) wave(2)
label variable s2oophos1y "s2oophos1y:w2 S hospitalization oop expenditure, prv yr"

***Out of Pocket Outpatient Care Expenditures***
*wave 2 respondent oop outpatient care expenditures
gen r2oopdoc1y = .
replace r2oopdoc1y = .m if hc047e == . & inw`wv' == 1
replace r2oopdoc1y = .d if hc047e == -9999991
replace r2oopdoc1y = .r if hc047e == -9999992
replace r2oopdoc1y = hc047e if inrange(hc047e,0,200000)
label variable r2oopdoc1y "r2oopdoc1y:w2 R doctor oop expenditure, prv yr"

*wave 2 spouse oop outpatient care expenditures
gen s2oopdoc1y = .
spouse r2oopdoc1y, result(s2oopdoc1y) wave(2)
label variable s2oopdoc1y "s2oopdoc1y:w2 S doctor oop expenditure, prv yr"

***Out of Pocket Drug Expenditures***Not in Codebook, Part of Sum
*wave 2 respondent oop drug expenditures
gen r2oopdrug1y = .
replace r2oopdrug1y = .m if hc049e == . & inw`wv' == 1
replace r2oopdrug1y = .d if hc049e == -9999991
replace r2oopdrug1y = .r if hc049e == -9999992
replace r2oopdrug1y = hc049e if inrange(hc049e,0,140000)
label variable r2oopdrug1y "r2oopdrug1y:w2 R drug oop expenditure, prv yr"

*wave 2 spouse oop drug expenditures
gen s2oopdrug1y = .
spouse r2oopdrug1y, result(s2oopdrug1y) wave(2)
label variable s2oopdrug1y "s2oopdrug1y:w2 S drug oop expenditure, prv yr"

***Out of Pocket Day Care/Nursing Home/Home Care Expenditures***Not in Codebook, Part of Sum
*wave 2 respondent oop day care/nursing home/home care expenditures
gen r2oophmcr1y = .
replace r2oophmcr1y = .m if hc051e == . & inw`wv' == 1
replace r2oophmcr1y = .d if hc051e == -9999991
replace r2oophmcr1y = .r if hc051e == -9999992
replace r2oophmcr1y = hc051e if inrange(hc051e,0,60000)
replace r2oophmcr1y = 0 if hc029_ == 5 | hc032dno == 1
label variable r2oophmcr1y "r2oophmcr1y:w2 R home care/nursing home oop expenditure, prv yr"

*wave 2 spouse oop day care/nursing home/home care expenditures
gen s2oophmcr1y = .
spouse r2oophmcr1y, result(s2oophmcr1y) wave(2)
label variable s2oophmcr1y "s2oophmcr1y:w2 S home care/nursing home oop expenditure, prv yr"

***Total Out of Pocket Medical Expenditures***
*wave 2 respondent total oop medical expenditures
egen r2oopmd1y = rowtotal(r2oophos1y r2oopdoc1y r2oopdrug1y r2oophmcr1y),m
replace r2oopmd1y = .m if r2oopmd1y == . & inw`wv' == 1
replace r2oopmd1y = .d if r2oopmd1y == .m & r2oophos1y==.d & r2oopdoc1y==.d & r2oopdrug1y==.d & r2oophmcr1y==.d
replace r2oopmd1y = .r if r2oopmd1y == .m & r2oophos1y==.r & r2oopdoc1y==.r & r2oopdrug1y==.r & r2oophmcr1y==.r
label variable r2oopmd1y "r2oopmd1y:w2 R oop medical expenditures, prv yr"

*wave 2 spouse total oop medical expenditures
gen s2oopmd1y = .
spouse r2oopmd1y, result(s2oopmd1y) wave(2)
label variable s2oopmd1y "s2oopmd1y:w2 S oop medical expenditures, prv yr"

***Whether Covered by Life Insurance***
*wave 2 HH cover by life insurance
replace as067_=0 if as067_==5
egen h2lifein=max(as067_) if !mi(hhid2), by(hhid2)
replace h2lifein = .m if missing(h2lifein) & inw`wv' == 1
replace h2lifein=as067_ if as067_==0 & h2lifein==1
replace h2lifein=as067_ if as067_==1 & h2lifein==0
replace h2lifein=.d if h2lifein==-1
replace h2lifein=.r if h2lifein==-2
label variable h2lifein "h2lifein:w2 cover by life insurance"
label values h2lifein ins


***drop wave 2 Health utlization file raw variables
drop `ins_w2_hc'

***drop wave 2 asset file raw variables
drop `ins_w2_as'

***drop wave 2 physical health file raw variables
drop `ins_w2_ph'

***drop wave 2 technical variables file raw variables
drop `ins_w2_techvar'

******************************************************************************************

*set wave number
local wv = 4

***merge with wave 4 Health utlization data***
local ins_w4_hc  country hc012_ hc013_ hc014_ hc029_ hc030_ hc031_ hc002_
merge 1:1 mergeid using "$wave_4_hc", keepusing(`ins_w4_hc') nogen

***merge with wave 4 asset data***
local ins_w4_as as067_ 
merge 1:1 mergeid using "$wave_4_as", keepusing(`ins_w4_as') nogen

***merge with wave 4 physical health data***
local ins_w4_ph  ph011d1 ph011d2 ph011d3 ph011d4 ph011d5 ph011d6 ph011d7 ph011d8 /// 
									ph011d9 ph011d10 ph011d11 ph011d12 ph011d13 ph011d14 ph011dot
merge 1:1 mergeid using "$wave_4_ph", keepusing(`ins_w4_ph') nogen


***Whether Any Hospital Stay***
*wave 4 respondent any hospital stay
gen r4hosp1y=.
missing_common hc012_,result(r4hosp1y)
replace r4hosp1y=1 if hc012_==1
replace r4hosp1y=0 if hc012_==5
label variable r4hosp1y "r4hosp1y:w4 R Hospital stay, pr 12 mos"
label values r4hosp1y diff

*wave 4 spouse any hospital stay
gen s4hosp1y=.
spouse r4hosp1y, result(s4hosp1y) wave(4)
label variable s4hosp1y "s4hosp1y:w4 S Hospital stay, pr 12 mos"
label values s4hosp1y diff

***Number Hospital Stays***
*wave 4 respondent # hospital stays
gen r4hsptim1y=.
missing_common hc013_,result(r4hsptim1y)
replace r4hsptim1y=0 if hc012==5
replace r4hsptim1y=hc013_ if inrange(hc013_,0,10)
label variable r4hsptim1y "r4hsptim1y:w4 R # Hospital stays, pr 12 mos"

*wave 4 spouse # hospital stays
gen s4hsptim1y=.
spouse r4hsptim1y, result(s4hsptim1y) wave(4)
label variable s4hsptim1y "s4hsptim1y:w4 S # Hospital stays, pr 12 mos"

***Number of Nights in Hospital***
*wave 4 respondent # nights in hospital 
gen r4hspnit1y=.
missing_common hc014_,result(r4hspnit1y)
replace r4hspnit1y=0 if hc012==5
replace r4hspnit1y=hc014_ if inrange(hc014_,0,365)
label variable r4hspnit1y "r4hspnit1y:w4 R # Nights in hosp, pr 12 mos"

*wave 4 spouse # nights in hospital 
gen s4hspnit1y=.
spouse r4hspnit1y, result(s4hspnit1y) wave(4)
label variable s4hspnit1y "s4hspnit1y:w4 S # Nights in hosp, pr 12 mos"

***Whether Any Nursing Home Stay***
*wave 4 respondent Nursing home stay
gen r4nrshom1y=.
missing_common hc029_,result(r4nrshom1y)
replace r4nrshom1y=1 if inlist(hc029_,1,3)
replace r4nrshom1y=0 if hc029_==5
label variable r4nrshom1y "r4nrshom1y:w4 R Nurs home stay, pr 12 mos"
label values r4nrshom1y diff

*wave 4 spouse Nursing home stay
gen s4nrshom1y=.
spouse r4nrshom1y, result(s4nrshom1y) wave(4)
label variable s4nrshom1y "s4nrshom1y:w4 S Nurs home stay, pr 12 mos"
label values s4nrshom1y diff

***Number of Nursing Home Stays***
*wave 4 respondent # Nursing home stays
gen r4nrstim1y=.
missing_common hc030_,result(r4nrstim1y)
replace r4nrstim1y=.n if hc029_==3
replace r4nrstim1y=0 if hc029_==5
replace r4nrstim1y=hc030_ if inrange(hc030_,1,365)
label variable r4nrstim1y "r4nrstim1y:w4 R # Nurs home stays, pr 12 mos"

*wave 4 spouse # Nursing home stays
gen s4nrstim1y=.
spouse r4nrstim1y, result(s4nrstim1y) wave(4)
label variable s4nrstim1y "s4nrstim1y:w4 S # Nurs home stays, pr 12 mos"

***Number Nights in Nursing Home***
*wave 4 respondent # Nights in nurs home
gen r4nrsnit1y=.
missing_common hc031_,result(r4nrsnit1y)
replace r4nrsnit1y=365 if hc029_==3
replace r4nrsnit1y=0 if hc029_==5
replace r4nrsnit1y=hc031_*7 if inrange(hc031_,1,52)
label variable r4nrsnit1y "r4nrsnit1y:w4 R # Nights in Nurs home, pr 12 mos"

*wave 4 spouse # Nights in nurs home
gen s4nrsnit1y=.
spouse r4nrsnit1y, result(s4nrsnit1y) wave(4)
label variable s4nrsnit1y "s4nrsnit1y:w4 S # Nights in Nurs home, pr 12 mos"

***Whether Lives in Nursing Home***
*wave 4 respondent Live in Nursing home 
gen r4nhmliv1y=.
missing_common hc029_,result(r4nhmliv1y)
replace r4nhmliv1y=1 if inlist(hc029_,3)
replace r4nhmliv1y=0 if inlist(hc029_,1,5)
label variable r4nhmliv1y "r4nhmliv1y:w4 R Live in Nurs home, pr 12 mos"
label values r4nhmliv1y diff

*wave 4 spouse Live in Nursing home 
gen s4nhmliv1y=.
spouse r4nhmliv1y, result(s4nhmliv1y) wave(4)
label variable s4nhmliv1y "s4nhmliv1y:w4 S R Live in Nurs home, pr 12 mos"
label values s4nhmliv1y diff

***Whether Any Doctor Visits***
*wave 4 respondent any doctor1y visit
gen r4doctor1y=.
missing_common hc002_,result(r4doctor1y)
replace r4doctor1y=1 if inrange(hc002_,1,98)
replace r4doctor1y=0 if hc002_==0
label variable r4doctor1y "r4doctor1y:w4 R doctor1y visit, pr 12 mos"
label values r4doctor1y diff

*wave 4 spouse any doctor1y visit
gen s4doctor1y=.
spouse r4doctor1y, result(s4doctor1y) wave(4)
label variable s4doctor1y "s4doctor1y:w4 S doctor1y visit, pr 12 mos"
label values s4doctor1y diff

***Number of Doctors Visits***
*wave 4 respondent # of times doctor1y visit
gen r4doctim1y=.
missing_common hc002_,result(r4doctim1y)
replace r4doctim1y=hc002_ if inrange(hc002_,0,98)
label variable r4doctim1y "r4doctim1y:w4 R # doctor1y visits, pr 12 mos"

*wave 4 spouse # of times doctor1y visit
gen s4doctim1y=.
spouse r4doctim1y, result(s4doctim1y) wave(4)
label variable s4doctim1y "s4doctim1y:w4 S # doctor1y visits, pr 12 mos"

*wave 4 respondent home hlth care
**no home care

*wave 4 respondent receive care from private provider
**not asked

***Whether Taking Any Drugs***
*wave 4 respondent takes any drugs
gen r4drugs1w = .
missing_common ph011d1 ph011d2 ph011d3 ph011d4 ph011d5 ph011d6 ph011d7 ph011d8 ph011d9 ///
		 ph011d10 ph011d11 ph011d12 ph011d13 ph011d14 ph011dot, result(r4drugs1w)
replace r4drugs1w = 0 if ph011d1==0|ph011d2==0|ph011d3==0|ph011d4==0|ph011d5==0|ph011d6==0|ph011d7==0| ///
		ph011d8==0|ph011d9==0|ph011d10==0|ph011d11==0|ph011d12==0|ph011d13==0|ph011d14==0|ph011dot==0
replace r4drugs1w = 1 if ph011d1==1|ph011d2==1|ph011d3==1|ph011d4==1|ph011d5==1|ph011d6==1|ph011d7==1| ///
		ph011d8==1|ph011d9==1|ph011d10==1|ph011d11==1|ph011d12==1|ph011d13==1|ph011d14==1|ph011dot==1
label variable r4drugs1w "r4drugs1w:w4 R reg take rx, prv week"
label values r4drugs1w diff

*wave 4 spouse takes any drugs
gen s4drugs1w = .
spouse r4drugs1w, result(s4drugs1w) wave(4)
label variable s4drugs1w "s4drugs1w:w4 S reg take rx, prv week"
label values s4drugs1w diff

***Whether Covered by Life Insurance***
*wave 4 HH cover by life insurance
replace as067_=0 if as067_==5
egen h4lifein=max(as067_) if !mi(hhid4), by(hhid4)
replace h4lifein = .m if missing(h4lifein) & inw`wv' == 1
replace h4lifein=as067_ if as067_==0 & h4lifein==1
replace h4lifein=as067_ if as067_==1 & h4lifein==0
replace h4lifein=.d if h4lifein==-1
replace h4lifein=.r if h4lifein==-2
label variable h4lifein "h4lifein:w4 cover by life insurance"
label values h4lifein ins


***drop wave 4 Health utlization file raw variables
drop `ins_w4_hc'

***drop wave 4 asset file raw variables
drop `ins_w4_as'

***drop wave 4 physical health file raw variables
drop `ins_w4_ph'

******************************************************************************************

*set wave number
local wv = 5

***merge with wave 5 Health utilization data***
local health_w5_hc country hc010_ hc012_ hc013_ hc014_ hc029_ hc031_ hc002_ /// ***hc030_
				   hc127d1 hc127d2 hc127d3 hc127d4 hc127dno hc082_ hc083e ///
				   hc088_ hc089e hc092_ hc093e hc094_ hc095e hc096_ hc097e  hc128_ hc129e 
merge 1:1 mergeid using "$wave_5_hc", keepusing(`health_w5_hc') nogen

***merge with wave 5 asset data***
local ins_w5_as as067_ 
merge 1:1 mergeid using "$wave_5_as", keepusing(`ins_w5_as') nogen

***merge with wave 5 physical health data***
local ins_w5_ph  ph011d1 ph011d2 ph011d3 ph011d4 ph011d6 ph011d7 ph011d8 /// 
									ph011d9 ph011d10 ph011d11 ph011d13 ph011d14 ph011d15 ph011dno ph011dot
merge 1:1 mergeid using "$wave_5_ph", keepusing(`ins_w5_ph') nogen


***Whether Any Hospital Stay***
*wave 5 respondent any hospital stay
gen r5hosp1y=.
missing_common hc012_,result(r5hosp1y)
replace r5hosp1y=1 if hc012_==1
replace r5hosp1y=0 if hc012_==5
label variable r5hosp1y "r5hosp1y:w5 R Hospital stay, pr 12 mos"
label values r5hosp1y diff

*wave 5 spouse any hospital stay
gen s5hosp1y=.
spouse r5hosp1y, result(s5hosp1y) wave(5)
label variable s5hosp1y "s5hosp1y:w5 S Hospital stay, pr 12 mos"
label values s5hosp1y diff

***Number of Hospital Stays***
*wave 5 respondent # hospital stays
gen r5hsptim1y=.
missing_common hc013_,result(r5hsptim1y)
replace r5hsptim1y=hc013_ if inrange(hc013_,0,10)
replace r5hsptim1y=0 if hc012==5
label variable r5hsptim1y "r5hsptim1y:w5 R # Hospital stays, pr 12 mos"

*wave 5 spouse # hospital stays
gen s5hsptim1y=.
spouse r5hsptim1y, result(s5hsptim1y) wave(5)
label variable s5hsptim1y "s5hsptim1y:w5 S # Hospital stays, pr 12 mos"

***Number Nights in Hospital***
*wave 5 respondent # nights in hospital 
gen r5hspnit1y=.
missing_common hc014_,result(r5hspnit1y)
replace r5hspnit1y=hc014_ if inrange(hc014_,0,365)
replace r5hspnit1y=0 if hc012==5
label variable r5hspnit1y "r5hspnit1y:w5 R # Nights in hosp, pr 12 mos"

*wave 5 spouse # nights in hospital 
gen s5hspnit1y=.
spouse r5hspnit1y, result(s5hspnit1y) wave(5)
label variable s5hspnit1y "s5hspnit1y:w5 S # Nights in hosp, pr 12 mos"

***Whether Any Nursing Home Stay***
*wave 5 respondent Nursing home stay
gen r5nrshom1y=.
missing_common hc029_,result(r5nrshom1y)
replace r5nrshom1y=1 if inlist(hc029_,1,3)
replace r5nrshom1y=0 if hc029_==5
label variable r5nrshom1y "r5nrshom1y:w5 R Nurs home stay, pr 12 mos"
label values r5nrshom1y diff

*wave 5 spouse Nursing home stay
gen s5nrshom1y=.
spouse r5nrshom1y, result(s5nrshom1y) wave(5)
label variable s5nrshom1y "s5nrshom1y:w5 S Nurs home stay, pr 12 mos"
label values s5nrshom1y diff

***Number Nights in Nursing Home***
*wave 5 respondent # Nights in nurs home
gen r5nrsnit1y=.
missing_common hc031_,result(r5nrsnit1y)
replace r5nrsnit1y=hc031_*7 if inrange(hc031_,1,52)
replace r5nrsnit1y=365 if hc029_==3
replace r5nrsnit1y=0 if hc029_==5
label variable r5nrsnit1y "r5nrsnit1y:w5 R # Nights in Nurs home, pr 12 mos"

*wave 5 spouse # Nights in nurs home
gen s5nrsnit1y=.
spouse r5nrsnit1y, result(s5nrsnit1y) wave(5)
label variable s5nrsnit1y "s5nrsnit1y:w5 S # Nights in Nurs home, pr 12 mos"

***Whether Lives in Nursing Home***
*wave 5 respondent Live in Nursing home 
gen r5nhmliv1y=.
missing_common hc029_,result(r5nhmliv1y)
replace r5nhmliv1y=1 if inlist(hc029_,3)
replace r5nhmliv1y=0 if inlist(hc029_,1,5)
label variable r5nhmliv1y "r5nhmliv1y:w5 R Live in Nurs home, pr 12 mos"
label values r5nhmliv1y diff

*wave 5 spouse Live in Nursing home 
gen s5nhmliv1y=.
spouse r5nhmliv1y, result(s5nhmliv1y) wave(5)
label variable s5nhmliv1y "s5nhmliv1y:w5 S R Live in Nurs home, pr 12 mos"
label values s5nhmliv1y diff

***Whether Any Doctor Visit***
*wave 5 respondent any doctor1y visit
gen r5doctor1y=.
missing_common hc002_,result(r5doctor1y)
replace r5doctor1y=1 if inrange(hc002_,1,98)
replace r5doctor1y=0 if hc002_==0
label variable r5doctor1y "r5doctor1y:w5 R doctor1y visit, pr 12 mos"
label values r5doctor1y diff

*wave 5 spouse any doctor1y visit
gen s5doctor1y=.
spouse r5doctor1y, result(s5doctor1y) wave(5)
label variable s5doctor1y "s5doctor1y:w5 S doctor1y visit, pr 12 mos"
label values s5doctor1y diff

***Number of Doctors Visits***
*wave 5 respondent # of times doctor1y visit
gen r5doctim1y=.
missing_common hc002_,result(r5doctim1y)
replace r5doctim1y = hc002_ if inrange(hc002_,0,98)
label variable r5doctim1y "r5doctim1y:w5 R # doctor1y visits, pr 12 mos"

*wave 5 spouse # of times doctor1y visit
gen s5doctim1y=.
spouse r5doctim1y, result(s5doctim1y) wave(5)
label variable s5doctim1y "s5doctim1y:w5 S # doctor1y visits, pr 12 mos"

***Whether Any Home Health Care***
*wave 5 respondent home hlth care
gen r5homcar1y=.
replace r5homcar1y = .m if (missing(hc127d1) | missing(hc127d2) | missing(hc127d3) ///
	| missing(hc127d4) | missing(hc127dno)) & inw`wv' == 1
replace r5homcar1y=.d  if hc127d1==-1 & hc127d2==-1 & hc127d3==-1 & hc127d4==-1
replace r5homcar1y=.r  if hc127d1==-2 & hc127d2==-2 & hc127d3==-2 & hc127d4==-2
replace r5homcar1y=.n if hc029==3
replace r5homcar1y=1 if hc127d1==1 | hc127d2==1 | hc127d3==1 | hc127d4==1
replace r5homcar1y=0 if hc127dno==1
lab var r5homcar1y "r5homcar1y:w5 R Home hlth care, pr 12 mos"
lab val r5homcar1y diff

*wave 5 spouse home hlth care
gen s5homcar1y=.
spouse r5homcar1y, result(s5homcar1y) wave(5)
label variable s5homcar1y "s5homcar1y:w5 S Home hlth care, pr 12 mos"
lab val s5homcar1y diff

*wave 5 respondent receive care from private provider
**not asked

***Whether Taking Any Drugs***
*wave 5 respondent takes any drugs
gen r5drugs1w = .
missing_common ph011d1 ph011d2 ph011d3 ph011d4 ph011d6 ph011d7 ph011d8 ph011d9 ///
		 ph011d10 ph011d11 ph011d13 ph011d14 ph011d15 ph011dot, result(r5drugs1w)
replace r5drugs1w = 0 if ph011dno==1
replace r5drugs1w = 1 if ph011d1==1 | ph011d2==1 | ph011d3==1 | ph011d4==1 | ph011d6==1 | ph011d7==1 | ///
		ph011d8==1 | ph011d9==1 | ph011d10==1 | ph011d11==1 | ph011d13==1 | ph011d14==1 | ph011d15==1 | ph011dot==1
label variable r5drugs1w "r5drugs1w:w5 R reg take rx, prv week"
label values r5drugs1w diff

*wave 5 spouse takes any drugs
gen s5drugs1w = .
spouse r5drugs1w, result(s5drugs1w) wave(5)
label variable s5drugs1w "s5drugs1w:w5 S reg take rx, prv week"
label values s5drugs1w diff

***Whether Had Dental Visit***
*wave 5 respondent had dental visit
gen r5dentst1y = .
missing_common hc010_, result(r5dentst1y)
replace r5dentst1y = 0 if hc010_ == 5
replace r5dentst1y = 1 if hc010_ == 1
label variable r5dentst1y "r5dentst1y:w5 R dental visit, prv yr"
label values r5dentst1y diff

*wave 5 spouse had dental visit
gen s5dentst1y = .
spouse r5dentst1y, result(s5dentst1y) wave(5)
label variable s5dentst1y "s5dentst1y:w5 S dental visit, prv yr"
label values s5dentst1y diff

***Out of Pocket Hospital & Facility Expenditures***
*wave 5 respondent oop hospital & facility expenditures
gen r5oophos1y = .
replace r5oophos1y = .m if hc095e == . & inw`wv' == 1
replace r5oophos1y = .d if hc095e == -9999991
replace r5oophos1y = .r if hc095e == -9999992
replace r5oophos1y = hc095e if inrange(hc095e,0,30000)
replace r5oophos1y = 0 if hc012_ == 5 | hc094_ == 5
label variable r5oophos1y "r5oophos1y:w5 R hospitalization oop expenditure, prv yr"

*wave 5 spouse oop hospital & facility expenditures
gen s5oophos1y = .
spouse r5oophos1y, result(s5oophos1y) wave(5)
label variable s5oophos1y "s5oophos1y:w5 S hospitalization oop expenditure, prv yr"

***Out of Pocket Doctor Expenditures***
*wave 5 respondent oop doctor care expenditures
gen r5oopdoc1y = .
replace r5oopdoc1y = .m if hc083e == . & inw`wv' == 1
replace r5oopdoc1y = .d if hc083e == -9999991
replace r5oopdoc1y = .r if hc083e == -9999992
replace r5oopdoc1y = hc083e if inrange(hc083e,0,400000)
replace r5oopdoc1y = .m if (hc083e<0 | hc083e>1000000) & !mi(hc083e)
replace r5oopdoc1y = 0 if hc082_ == 5 | hc002_ == 0
label variable r5oopdoc1y "r5oopdoc1y:w5 R doctor oop expenditure, prv yr"

*wave 5 spouse oop doctor expenditures
gen s5oopdoc1y = .
spouse r5oopdoc1y, result(s5oopdoc1y) wave(5)
label variable s5oopdoc1y "s5oopdoc1y:w5 S doctor oop expenditure, prv yr"

***Out of Pocket Drug Expenditures***
*wave 5 respondent oop drug expenditures
gen r5oopdrug1y = .
replace r5oopdrug1y = .m if hc089e == . & inw`wv' == 1
replace r5oopdrug1y = .d if hc089e == -9999991
replace r5oopdrug1y = .r if hc089e == -9999992
replace r5oopdrug1y = hc089e if inrange(hc089e,0,100000)
replace r5oopdrug1y = .m if hc089e>900000 & !mi(hc089e)
replace r5oopdrug1y = 0 if hc088_ == 5 | hc002_ == 0
label variable r5oopdrug1y "r5oopdrug1y:w5 R drug oop expenditure, prv yr"

*wave 5 spouse oop drug expenditures
gen s5oopdrug1y = .
spouse r5oopdrug1y, result(s5oopdrug1y) wave(5)
label variable s5oopdrug1y "s5oopdrug1y:w5 S drug oop expenditure, prv yr"

***Out of Pocket Dental Expenditures***
*wave 5 respondent oop dental expenditures
gen r5oopden1y = .
replace r5oopden1y = .m if hc093e == . & inw`wv' == 1
replace r5oopden1y = .d if hc093e == -9999991
replace r5oopden1y = .r if hc093e == -9999992
replace r5oopden1y = hc093e if inrange(hc093e,0,300000)
replace r5oopden1y = .m if hc093e>1000000 & !mi(hc093e)
replace r5oopden1y = 0 if hc092_ == 5 | hc010_ == 5
label variable r5oopden1y "r5oopden1y:w5 R dental oop expenditure, prv yr"

*wave 5 spouse oop dental expenditures
gen s5oopden1y = .
spouse r5oopden1y, result(s5oopden1y) wave(5)
label variable s5oopden1y "s5oopden1y:w5 S dental oop expenditure, prv yr"

***Out of Pocket Home Care Expenditures***Not in Codebook, Part of Sum
*wave 5 respondent oop home care expenditures
gen r5oophome1y = .
replace r5oophome1y = .m if hc129e == . & inw`wv' == 1
replace r5oophome1y = .d if hc129e == -9999991
replace r5oophome1y = .r if hc129e == -9999992
replace r5oophome1y = hc129e if inrange(hc129e,0,50000)
replace r5oophome1y = .m if hc129e>1000000 & !mi(hc129e)
replace r5oophome1y = 0 if hc029_ == 5 | hc128_ == 5 | hc127dno == 1
label variable r5oophome1y "r5oophome1y:w5 R home care oop expenditure, prv yr"

*wave 5 spouse oop home care expenditures
gen s5oophome1y = .
spouse r5oophome1y, result(s5oophome1y) wave(5)
label variable s5oophome1y "s5oophome1y:w5 S home care oop expenditure, prv yr"

***Out of Pocket Nursing Home Expenditures***Not in Codebook, Part of Sum
*wave 5 respondent oop nursing home expenditures
gen r5oopnurs1y = .
replace r5oopnurs1y = .m if hc097e == . & inw`wv' == 1
replace r5oopnurs1y = .d if hc097e == -9999991
replace r5oopnurs1y = .r if hc097e == -9999992
replace r5oopnurs1y = hc097e if inrange(hc097e,0,50000)
replace r5oopnurs1y = 0 if hc096_ == 5 | hc029_ == 5 
label variable r5oopnurs1y "r5oopnurs1y:w5 R nursing home oop expenditure, prv yr"

*wave 5 spouse oop nursing home expenditures
gen s5oopnurs1y = .
spouse r5oopnurs1y, result(s5oopnurs1y) wave(5)
label variable s5oopnurs1y "s5oopnurs1y:w5 S nursing home oop expenditure, prv yr"

***Out of Pocket Home Care/Nursing Home Expenditures***
*wave 5 respondent oop home care/nursing home expenditures
egen r5oophmcr1y = rowtotal(r5oophome1y r5oopnurs1y),m
replace r5oophmcr1y = .m if r5oophmcr1y == . & inw`wv' == 1
replace r5oophmcr1y = .d if r5oophmcr1y == .m & r5oophome1y == .d & r5oopnurs1y == .d
replace r5oophmcr1y = .r if r5oophmcr1y == .m & r5oophome1y == .r & r5oopnurs1y == .r
label variable r5oophmcr1y "r5oophmcr1y:w5 R home care/nursing home oop expenditure, prv yr"

*wave 5 spouse oop home care/nursing home expenditures
gen s5oophmcr1y = .
spouse r5oophmcr1y, result(s5oophmcr1y) wave(5)
label variable s5oophmcr1y "s5oophmcr1y:w5 S home care/nursing home oop expenditure, prv yr"

***Total Out of Pocket Medical Expenditures***
*wave 5 respondent total oop medical expenditures
egen r5oopmd1y = rowtotal(r5oophos1y r5oopdoc1y r5oopdrug1y r5oopden1y r5oophome1y r5oopnurs1y),m
replace r5oopmd1y = .m if r5oopmd1y == . & inw`wv' == 1
replace r5oopmd1y = .d if r5oopmd1y == .m & r5oophos1y==.d & r5oopdoc1y==.d & r5oopdrug1y==.d & r5oopden1y==.d & r5oophome1y==.d & r5oopnurs1y==.d
replace r5oopmd1y = .r if r5oopmd1y == .m & r5oophos1y==.r & r5oopdoc1y==.r & r5oopdrug1y==.r & r5oopden1y==.r & r5oophome1y==.r & r5oopnurs1y==.r
label variable r5oopmd1y "r5oopmd1y:w5 R oop medical expenditures, prv yr"

*wave 5 spouse total oop medical expenditures
gen s5oopmd1y = .
spouse r5oopmd1y, result(s5oopmd1y) wave(5)
label variable s5oopmd1y "s5oopmd1y:w5 S oop medical expenditures, prv yr"

drop r5oophome1y r5oopnurs1y
drop s5oophome1y s5oopnurs1y

***Whether Covered by Life Insurance***
*wave 5 HH cover by life insurance
replace as067_=0 if as067_==5
egen h5lifein=max(as067_) if !mi(hhid5), by(hhid5)
replace h5lifein = .m if missing(h5lifein) & inw`wv' == 1
replace h5lifein=as067_ if as067_==0 & h5lifein==1
replace h5lifein=as067_ if as067_==1 & h5lifein==0
replace h5lifein=.d if h5lifein==-1
replace h5lifein=.r if h5lifein==-2
label variable h5lifein "h5lifein:w5 cover by life insurance"
label values h5lifein ins


***drop wave 5 Health utlization file raw variables
drop `health_w5_hc'

***drop wave 5 asset file raw variables
drop `ins_w5_as'

***drop wave 5 physical health file raw variables
drop `ins_w5_ph'

******************************************************************************************


*set wave number
local wv = 6

***merge with wave 6 Health utilization data***
local health_w6_hc country hc010_ hc012_ hc013_ hc014_ hc029_ hc031_ hc602_ /// ***hc030_
				   hc127d1 hc127d2 hc127d3 hc127d4 hc127dno hc682_ hc683e ///
				   hc688_ hc689e hc692_ hc693e hc694_ hc695e hc696_ hc097e hc628_ hc629e 
merge 1:1 mergeid using "$wave_6_hc", keepusing(`health_w6_hc') nogen

***merge with wave 6 asset data***
local ins_w6_as as067_ 
merge 1:1 mergeid using "$wave_6_as", keepusing(`ins_w6_as') nogen

***merge with wave 6 physical health data***
local ins_w6_ph  ph011d1 ph011d2 ph011d3 ph011d4 ph011d6 ph011d7 ph011d8 /// 
									ph011d9 ph011d10 ph011d11 ph011d13 ph011d14 ph011d15 ph011dno ph011dot
merge 1:1 mergeid using "$wave_6_ph", keepusing(`ins_w6_ph') nogen


***Whether Any Hospital Stay***
*wave 6 respondent any hospital stay
gen r6hosp1y = .
missing_common hc012_,result(r6hosp1y)
replace r6hosp1y=1 if hc012_==1
replace r6hosp1y=0 if hc012_==5
label variable r6hosp1y "r6hosp1y:w6 R Hospital stay, pr 12 mos"
label values r6hosp1y diff

*wave 6 spouse any hospital stay
gen s6hosp1y = .
spouse r6hosp1y, result(s6hosp1y) wave(6)
label variable s6hosp1y "s6hosp1y:w6 S Hospital stay, pr 12 mos"
label values s6hosp1y diff

***Number of Hospital Stays***
*wave 6 respondent # hospital stays
gen r6hsptim1y = .
missing_common hc013_,result(r6hsptim1y)
replace r6hsptim1y=hc013_ if inrange(hc013_,0,10)
replace r6hsptim1y=0 if hc012==5
label variable r6hsptim1y "r6hsptim1y:w6 R # Hospital stays, pr 12 mos"

*wave 6 spouse # hospital stays
gen s6hsptim1y = .
spouse r6hsptim1y, result(s6hsptim1y) wave(6)
label variable s6hsptim1y "s6hsptim1y:w6 S # Hospital stays, pr 12 mos"

***Number Nights in Hospital***
*wave 6 respondent # nights in hospital 
gen r6hspnit1y = .
missing_common hc014_,result(r6hspnit1y)
replace r6hspnit1y=hc014_ if inrange(hc014_,0,365)
replace r6hspnit1y=0 if hc012==5
label variable r6hspnit1y "r6hspnit1y:w6 R # Nights in hosp, pr 12 mos"

*wave 6 spouse # nights in hospital 
gen s6hspnit1y = .
spouse r6hspnit1y, result(s6hspnit1y) wave(6)
label variable s6hspnit1y "s6hspnit1y:w6 S # Nights in hosp, pr 12 mos"

***Whether Any Nursing Home Stay***
*wave 6 respondent Nursing home stay
gen r6nrshom1y = .
missing_common hc029_,result(r6nrshom1y)
replace r6nrshom1y=1 if inlist(hc029_,1,3)
replace r6nrshom1y=0 if hc029_==5
label variable r6nrshom1y "r6nrshom1y:w6 R Nurs home stay, pr 12 mos"
label values r6nrshom1y diff

*wave 6 spouse Nursing home stay
gen s6nrshom1y= .
spouse r6nrshom1y, result(s6nrshom1y) wave(6)
label variable s6nrshom1y "s6nrshom1y:w6 S Nurs home stay, pr 12 mos"
label values s6nrshom1y diff

***Number Nights in Nursing Home***
*wave 6 respondent # Nights in nurs home
gen r6nrsnit1y = .
missing_common hc031_,result(r6nrsnit1y)
replace r6nrsnit1y=hc031_*7 if inrange(hc031_,1,52)
replace r6nrsnit1y=365 if hc029_==3
replace r6nrsnit1y=0 if hc029_==5
label variable r6nrsnit1y "r6nrsnit1y:w6 R # Nights in Nurs home, pr 12 mos"

*wave 6 spouse # Nights in nurs home
gen s6nrsnit1y = .
spouse r6nrsnit1y, result(s6nrsnit1y) wave(6)
label variable s6nrsnit1y "s6nrsnit1y:w6 S # Nights in Nurs home, pr 12 mos"

***Whether Lives in Nursing Home***
*wave 6 respondent Live in Nursing home 
gen r6nhmliv1y = .
missing_common hc029_,result(r6nhmliv1y)
replace r6nhmliv1y=1 if inlist(hc029_,3)
replace r6nhmliv1y=0 if inlist(hc029_,1,5)
label variable r6nhmliv1y "r6nhmliv1y:w6 R Live in Nurs home, pr 12 mos"
label values r6nhmliv1y diff

*wave 6 spouse Live in Nursing home 
gen s6nhmliv1y= .
spouse r6nhmliv1y, result(s6nhmliv1y) wave(6)
label variable s6nhmliv1y "s6nhmliv1y:w6 S R Live in Nurs home, pr 12 mos"
label values s6nhmliv1y diff

***Whether Any Doctor Visit***
*wave 6 respondent any doctor1y visit
gen r6doctor1y = .
missing_common hc602_,result(r6doctor1y)
replace r6doctor1y=1 if inrange(hc602_,1,365)
replace r6doctor1y=0 if hc602_==0
label variable r6doctor1y "r6doctor1y:w6 R doctor1y visit, pr 12 mos"
label values r6doctor1y diff

*wave 6 spouse any doctor1y visit
gen s6doctor1y = .
spouse r6doctor1y, result(s6doctor1y) wave(6)
label variable s6doctor1y "s6doctor1y:w6 S doctor1y visit, pr 12 mos"
label values s6doctor1y diff

***Number of Doctors Visits***
*wave 6 respondent # of times doctor1y visit
gen r6doctim1y = .
missing_common hc602_,result(r6doctim1y)
replace r6doctim1y = hc602_ if inrange(hc602_,0,98)
replace r6doctim1y = 98 if inrange(hc602_,99,365)
label variable r6doctim1y "r6doctim1y:w6 R # doctor1y visits, pr 12 mos"

*wave 6 spouse # of times doctor1y visit
gen s6doctim1y = .
spouse r6doctim1y, result(s6doctim1y) wave(6)
label variable s6doctim1y "s6doctim1y:w6 S # doctor1y visits, pr 12 mos"

***Whether Any Home Health Care***
*wave 6 respondent home hlth care
gen r6homcar1y = .
replace r6homcar1y = .m if (missing(hc127d1) | missing(hc127d2) | missing(hc127d3) ///
	| missing(hc127d4) | missing(hc127dno)) & inw`wv' == 1
replace r6homcar1y=.d  if hc127d1==-1 & hc127d2==-1 & hc127d3==-1 & hc127d4==-1
replace r6homcar1y=.r  if hc127d1==-2 & hc127d2==-2 & hc127d3==-2 & hc127d4==-2
replace r6homcar1y=.n if hc029==3
replace r6homcar1y=1 if hc127d1==1 | hc127d2==1 | hc127d3==1 | hc127d4==1
replace r6homcar1y=0 if hc127dno==1
lab var r6homcar1y "r6homcar1y:w6 R Home hlth care, pr 12 mos"
lab val r6homcar1y diff

*wave 6 spouse home hlth care
gen s6homcar1y = .
spouse r6homcar1y, result(s6homcar1y) wave(6)
label variable s6homcar1y "s6homcar1y:w6 S Home hlth care, pr 12 mos"
lab val s6homcar1y diff

*wave 6 respondent receive care from private provider
**not asked

***Whether Taking Any Drugs***
*wave 6 respondent takes any drugs
gen r6drugs1w = .
missing_common ph011d1 ph011d2 ph011d3 ph011d4 ph011d6 ph011d7 ph011d8 ph011d9 ///
		 ph011d10 ph011d11 ph011d13 ph011d14 ph011d15 ph011dot, result(r6drugs1w)
replace r6drugs1w = .m if ph011dno==0 & ph011d1==0 & ph011d2==0 & ph011d3==0 & ph011d4==0 & ph011d6==0 & ph011d7==0 & ///
		ph011d8==0 & ph011d9==0 & ph011d10==0 & ph011d11==0 & ph011d13==0 & ph011d14==0 & ph011d15==0 & ph011dot==0
replace r6drugs1w = 0 if ph011dno==1
replace r6drugs1w = 1 if ph011d1==1 | ph011d2==1 | ph011d3==1 | ph011d4==1 | ph011d6==1 | ph011d7==1 | ///
		ph011d8==1 | ph011d9==1 | ph011d10==1 | ph011d11==1 | ph011d13==1 | ph011d14==1 | ph011d15==1 | ph011dot==1
label variable r6drugs1w "r6drugs1w:w6 R reg take rx, prv week"
label values r6drugs1w diff

*wave 6 spouse takes any drugs
gen s6drugs1w = .
spouse r6drugs1w, result(s6drugs1w) wave(6)
label variable s6drugs1w "s6drugs1w:w6 S reg take rx, prv week"
label values s6drugs1w diff

***Whether Had Dental Visit***
*wave 6 respondent had dental visit
gen r6dentst1y = .
missing_common hc010_, result(r6dentst1y)
replace r6dentst1y = 0 if hc010_ == 5
replace r6dentst1y = 1 if hc010_ == 1
label variable r6dentst1y "r6dentst1y:w6 R dental visit, prv yr"
label values r6dentst1y diff

*wave 6 spouse had dental visit
gen s6dentst1y = .
spouse r6dentst1y, result(s6dentst1y) wave(6)
label variable s6dentst1y "s6dentst1y:w6 S dental visit, prv yr"
label values s6dentst1y diff

***Out of Pocket Hospital & Facility Expenditures***
*wave 6 respondent oop hospital & facility expenditures
gen r6oophos1y = .
replace r6oophos1y = .m if hc695e == . & inw`wv' == 1
replace r6oophos1y = .d if hc695e == -9999991
replace r6oophos1y = .r if hc695e == -9999992
replace r6oophos1y = hc695e if inrange(hc695e,0,500000)
replace r6oophos1y = 0 if hc012_ == 5 | hc694_ == 5
label variable r6oophos1y "r6oophos1y:w6 R hospitalization oop expenditure, prv yr"

*wave 6 spouse oop hospital & facility expenditures
gen s6oophos1y = .
spouse r6oophos1y, result(s6oophos1y) wave(6)
label variable s6oophos1y "s6oophos1y:w6 S hospitalization oop expenditure, prv yr"

***Out of Pocket Doctor Expenditures***
*wave 6 respondent oop doctor care expenditures
gen r6oopdoc1y = .
replace r6oopdoc1y = .m if hc683e == . & inw`wv' == 1
replace r6oopdoc1y = .d if hc683e == -9999991
replace r6oopdoc1y = .r if hc683e == -9999992
replace r6oopdoc1y = hc683e if inrange(hc683e,0,400000)
replace r6oopdoc1y = .m if (hc683e<0 | hc683e>1000000) & !mi(hc683e)
replace r6oopdoc1y = 0 if hc682_ == 5 | hc602_ == 0
label variable r6oopdoc1y "r6oopdoc1y:w6 R doctor oop expenditure, prv yr"

*wave 6 spouse oop doctor expenditures
gen s6oopdoc1y = .
spouse r6oopdoc1y, result(s6oopdoc1y) wave(6)
label variable s6oopdoc1y "s6oopdoc1y:w6 S doctor oop expenditure, prv yr"

***Out of Pocket Drug Expenditures***
*wave 6 respondent oop drug expenditures
gen r6oopdrug1y = .
replace r6oopdrug1y = .m if hc689e == . & inw`wv' == 1
replace r6oopdrug1y = .d if hc689e == -9999991
replace r6oopdrug1y = .r if hc689e == -9999992
replace r6oopdrug1y = hc689e if inrange(hc689e,0,200000)
replace r6oopdrug1y = .m if hc689e>900000 & !mi(hc689e)
replace r6oopdrug1y = 0 if hc688_ == 5 | hc602_ == 0
label variable r6oopdrug1y "r6oopdrug1y:w6 R drug oop expenditure, prv yr"

*wave 6 spouse oop drug expenditures
gen s6oopdrug1y = .
spouse r6oopdrug1y, result(s6oopdrug1y) wave(6)
label variable s6oopdrug1y "s6oopdrug1y:w6 S drug oop expenditure, prv yr"

***Out of Pocket Dental Expenditures***
*wave 6 respondent oop dental expenditures
gen r6oopden1y = .
replace r6oopden1y = .m if hc693e == . & inw`wv' == 1
replace r6oopden1y = .d if hc693e == -9999991
replace r6oopden1y = .r if hc693e == -9999992
replace r6oopden1y = hc693e if inrange(hc693e,0,300000)
replace r6oopden1y = .m if hc693e>1000000 & !mi(hc693e)
replace r6oopden1y = 0 if hc692_ == 5 | hc010_ == 5
label variable r6oopden1y "r6oopden1y:w6 R dental oop expenditure, prv yr"

*wave 6 spouse oop dental expenditures
gen s6oopden1y = .
spouse r6oopden1y, result(s6oopden1y) wave(6)
label variable s6oopden1y "s6oopden1y:w6 S dental oop expenditure, prv yr"

***Out of Pocket Home Care Expenditures***Not in Codebook, Part of Sum
*wave 6 respondent oop home care expenditures
gen r6oophome1y = .
replace r6oophome1y = .m if hc629e == . & inw`wv' == 1
replace r6oophome1y = .d if hc629e == -9999991
replace r6oophome1y = .r if hc629e == -9999992
replace r6oophome1y = hc629e if inrange(hc629e,0,200000)
replace r6oophome1y = .m if hc629e>1000000 & !mi(hc629e)
replace r6oophome1y = 0 if hc029_ == 5 | hc628_ == 5 | hc127dno == 1
label variable r6oophome1y "r6oophome1y:w6 R home care oop expenditure, prv yr"

*wave 6 spouse oop home care expenditures
gen s6oophome1y = .
spouse r6oophome1y, result(s6oophome1y) wave(6)
label variable s6oophome1y "s6oophome1y:w6 S home care oop expenditure, prv yr"

***Out of Pocket Nursing Home Expenditures***Not in Codebook, Part of Sum
*wave 6 respondent oop nursing home expenditures
gen r6oopnurs1y = .
replace r6oopnurs1y = .m if hc097e == . & inw`wv' == 1
replace r6oopnurs1y = .d if hc097e == -9999991
replace r6oopnurs1y = .r if hc097e == -9999992
replace r6oopnurs1y = hc097e if inrange(hc097e,0,50000)
replace r6oopnurs1y = 0 if hc696_ == 5 | hc029_ == 5 
label variable r6oopnurs1y "r6oopnurs1y:w6 R nursing home oop expenditure, prv yr"

*wave 6 spouse oop nursing home expenditures
gen s6oopnurs1y = .
spouse r6oopnurs1y, result(s6oopnurs1y) wave(6)
label variable s6oopnurs1y "s6oopnurs1y:w6 S nursing home oop expenditure, prv yr"

***Out of Pocket Home Care/Nursing Home Expenditures***
*wave 6 respondent oop home care/nursing home expenditures
egen r6oophmcr1y = rowtotal(r6oophome1y r6oopnurs1y),m
replace r6oophmcr1y = .m if r6oophmcr1y == . & inw`wv' == 1
replace r6oophmcr1y = .d if r6oophmcr1y == .m & r6oophome1y == .d & r6oopnurs1y == .d
replace r6oophmcr1y = .r if r6oophmcr1y == .m & r6oophome1y == .r & r6oopnurs1y == .r
label variable r6oophmcr1y "r6oophmcr1y:w6 R home care/nursing home oop expenditure, prv yr"

*wave 6 spouse oop home care/nursing home expenditures
gen s6oophmcr1y = .
spouse r6oophmcr1y, result(s6oophmcr1y) wave(6)
label variable s6oophmcr1y "s6oophmcr1y:w6 S home care/nursing home oop expenditure, prv yr"

***Total Out of Pocket Medical Expenditures***
*wave 6 respondent total oop medical expenditures
egen r6oopmd1y = rowtotal(r6oophos1y r6oopdoc1y r6oopdrug1y r6oopden1y r6oophome1y r6oopnurs1y),m
replace r6oopmd1y = .m if r6oopmd1y == . & inw`wv' == 1
replace r6oopmd1y = .d if r6oopmd1y == .m & r6oophos1y==.d & r6oopdoc1y==.d & r6oopdrug1y==.d & r6oopden1y==.d & r6oophome1y==.d & r6oopnurs1y==.d
replace r6oopmd1y = .r if r6oopmd1y == .m & r6oophos1y==.r & r6oopdoc1y==.r & r6oopdrug1y==.r & r6oopden1y==.r & r6oophome1y==.r & r6oopnurs1y==.r
label variable r6oopmd1y "r6oopmd1y:w6 R oop medical expenditures, prv yr"

*wave 6 spouse total oop medical expenditures
gen s6oopmd1y = .
spouse r6oopmd1y, result(s6oopmd1y) wave(6)
label variable s6oopmd1y "s6oopmd1y:w6 S oop medical expenditures, prv yr"

drop r6oophome1y r6oopnurs1y
drop s6oophome1y s6oopnurs1y

***Whether Covered by Life Insurance***
*wave 6 HH cover by life insurance
replace as067_=0 if as067_==5
egen h6lifein=max(as067_) if !mi(hhid6), by(hhid6)
replace h6lifein = .m if missing(h6lifein) & inw`wv' == 1
replace h6lifein=as067_ if as067_==0 & h6lifein==1
replace h6lifein=as067_ if as067_==1 & h6lifein==0
replace h6lifein=.d if h6lifein==-1
replace h6lifein=.r if h6lifein==-2
label variable h6lifein "h6lifein:w6 cover by life insurance"
label values h6lifein ins


***drop wave 6 Health utlization file raw variables
drop `health_w6_hc'

***drop wave 6 asset file raw variables
drop `ins_w6_as'

***drop wave 6 physical health file raw variables
drop `ins_w6_ph'

******************************************************************************************


*whether parent alive
label define liv ///
   0 "0.no"  ///
   1 "1.yes" ///
   .d ".d:DK" ///
   .m ".m:missing" ///
   .r ".r:refuse" ///
   .b ".b:baseline questionnaire"

*Number of living children imputation flag	
label define fchild ///
   0 "0.Not Imputed"  ///
   1 "1.Imputed" ///
   -8 "-8.Israel" ///
   -9 "-9.Ireland"
   
*Any child coresides/lives near
label define child ///
	0 "0.no child" ///
	1 "1.1+ child" ///
	.k ".k:no kids"
	
*Any weekly contact
label define contact ///
	0 "0.no weekly contact" ///
	1 "1.weekly contact" ///
	.k ".k:no kids" ///
	.n ".n:no living parents" ///
	.b ".b:baseline questionnaire"
	
*Frequency contact	
label define freqcont ///
	1 "1.daily" ///
	2 "2.several times a week" ///
	3 "3.about once a week" ///
	4 "4.about every two weeks" ///
	5 "5.about once a month" ///
	6 "6.less than once a month" ///
	7 "7.never" ///
	.k ".k:no kids" ///
	.n ".n:no living parents" ///
	.b ".b:baseline questionnaire"
	
*Any informal care given
label define careg ///
	0 "0.gave no care" ///
	1 "1.gave care" ///
	.k ".k:no kids" ///
	.g ".g:no grandchildren" ///
	.b ".b:non-responding spouse" ///
	.x ".x:none of these" 

*Any informal care received
label define carer ///
	0 "0.received no care" ///
	1 "1.received care" ///
	.k ".k:no kids" ///
	.b ".b:non-responding spouse" ///
	.x ".x:none of these"
	
*Frequency help
label define hoften ///
	1 "1.almost every day" ///
	2 "2.almost every week" ///
	3 "3.almost every month" ///
	4 "4.less often"

*Any transfer
label define trans ///
	0 "0.no transfer" ///
	1 "1.transfer" ///
	.k ".k:no kids/grandchildren" ///
	.n ".n:no living parents" ///
	.x ".x:none of these" ///
	.b ".b:baseline questionnaire"
	
*Any social activites
label define wksoc ///
	0 "0.no weekly activities" ///
	1 "1.weekly activites" ///
	.s ".s:not asked in this country or language"
	
*Any yearly social activities
label define yrsoc ///
	0 "0.no yearly activities" ///
	1 "1.yearly activities" ///
	.s ".s:not asked in this country or language" ///
	.p ".p:proxy" ///
	.n ".n:nursing home ivw"
	
*Frequency religious groups
label define relfreq ///
	1 "1.almost daily" ///
	2 "2.almost every week" ///
	3 "3.less often" ///
	4 "4.never"
 
******************************************************************************************

 
	

*set wave number
local wv = 1

***merge with wave 1 cover screen (respondent) data***
local family_w1_cv_r hhsize country relrpers gender
merge 1:1 mergeid using "$wave_1_cv_r", keepusing(`family_w1_cv_r') nogen

***merge with wave 1 Demographic data***
local family_w1_dn dn034_ dn036_ dn037_ dn026_1 dn026_2 dn027_1 dn027_2 dn028_1 dn028_2 dn032_1 dn032_2 dn030_1 dn030_2
merge 1:1 mergeid using "$wave_1_dn", keepusing(`family_w1_dn') nogen

***merge with wave 1 Children data***
local family_w1_ch ch001_ ch021_ ch005_* ch007_* ch014_*
merge 1:1 mergeid using "$wave_1_ch", keepusing(`family_w1_ch') nogen

***merge with wave 1 imputation data***
local family_w1_imput nchild nchild_f ngrchild ngrchild_f fam_resp fin_resp
merge 1:1 mergeid using "$wave_1_imputations_", keepusing(`family_w1_imput') nogen

***merge with wave 1 sp data***
local family_w1_sp sp002_ sp003_1 sp003_2 sp003_3 sp008_ sp009_1 sp009_2 sp009_3 sp014_ sp018_ sp020_ ///
		sp004d1_* sp004d2_* sp004d3_* sp019* sp021*
merge 1:1 mergeid using "$wave_1_sp", keepusing(`family_w1_sp') nogen

***merge with wave 1 ft data***
local family_w1_ft ft002_ ft003_* ft004e_* ft009_ ft010_* ft011e_*
merge 1:1 mergeid using "$wave_1_ft", keepusing(`family_w1_ft') nogen

***merge with wave 1 ac data***
local family_w1_ac ac002d* ac003_*
merge 1:1 mergeid using "$wave_1_ac", keepusing(`family_w1_ac') nogen

***merge with wave 1 physical health data***
local family_w1_ph ph048dno
merge 1:1 mergeid using "$wave_1_ph", keepusing(`family_w1_ph') nogen


***parental mortality: mother alive***
*wave 1 respondent mother alive status
tempvar mominhouse
egen `mominhouse' = total(relrpers == 5 & gender == 2), by(hhid1)
recode `mominhouse' (1/10 = 1)
gen r1momliv =.
missing_common dn026_1, result(r1momliv) wave(`wv')
replace r1momliv = 0 if dn026_1 == 5 
replace r1momliv = 1 if dn026_1 == 1 | `mominhouse' == 1 
label variable r1momliv "r1momliv:w1 r mother alive"
label values r1momliv  liv

*wave 1 spouse mother alive status
gen s1momliv =.
spouse r1momliv, result(s1momliv) wave(1)
label variable s1momliv "s1momliv:w1 s mother alive"
label values s1momliv liv

***parental mortality: father alive***
*wave 1 respondent father alive status
tempvar dadinhouse
egen `dadinhouse' = total(relrpers == 5 & gender == 1), by(hhid1)
recode `dadinhouse' (1/10 = 1)
gen r1dadliv =.
missing_common dn026_2, result(r1dadliv) wave(`wv')
replace r1dadliv = 0 if dn026_2 == 5 
replace r1dadliv = 1 if dn026_2 == 1 | `dadinhouse' == 1 
label variable r1dadliv "r1dadliv:w1 r father alive"
label values r1dadliv liv

*wave 1 spouse father alive status
gen s1dadliv =.
spouse r1dadliv, result(s1dadliv) wave(1)
label variable s1dadliv "s1dadliv:w1 s father alive"
label values s1dadliv liv

***parental mortality: mother's current age or age at death***
*wave 1 respondent mother's age
gen r1momage =.
missing_common dn026_1, result(r1momage) wave(`wv')
missing_common dn028_1 if dn026_1 == 1, result(r1momage) wave(`wv')
missing_common dn027_1 if dn026_1 == 5, result(r1momage) wave(`wv')
replace r1momage = dn028_1 if dn026_1 == 1 & inrange(dn028_1, 1, 120)
replace r1momage = dn027_1 if dn026_1 == 5 & inrange(dn027_1, 1, 120)
label variable r1momage "r1momage:w1 r mother age current/at death"

*wave 1 spouse mother's age
gen s1momage =.
spouse r1momage, result(s1momage) wave(1)
label variable s1momage "s1momage:w1 s mother age current/at death"

***parental mortality: father's current age or age at death***
*wave 1 respondent father's age
gen r1dadage =.
missing_common dn026_2, result(r1dadage) wave(`wv')
missing_common dn028_2 if dn026_2 == 1, result(r1dadage) wave(`wv')
missing_common dn027_2 if dn026_2 == 5, result(r1dadage) wave(`wv')
replace r1dadage = dn028_2 if dn026_2 == 1 & inrange(dn028_2, 1, 120)
replace r1dadage = dn027_2 if dn026_2 == 5 & inrange(dn027_2, 1, 120)
label variable r1dadage "r1dadage:w1 r father age current/at death"

*wave 1 spouse father's age
gen s1dadage =.
spouse r1dadage, result(s1dadage) wave(1)
label variable s1dadage "s1dadage:w1 s father age current/at death"

***Number of people living in house***
*wave 1 household number of people living in house
gen hh1hhres = hhsize if inw1==1
label variable hh1hhres "hh1hhres:w1 Number of people in HH"

***Number of children***
*wave 1 household number of living children
gen h`wv'child = nchild
label variable h`wv'child "h`wv'child:w`wv' Number of living children R/P"

*wave 1 couple-level flag number of living children
chimputation_flag nchild_f, gen(h`wv'fchild) by(h`wv'coupid)
label variable h`wv'fchild "h`wv'fchild:w`wv' Impute Flag Num of living children"
label values h`wv'fchild fchild

***Number of grandchildren***
*wave 1 household number of grandchildren
gen h`wv'grchild = ngrchild
label variable h`wv'grchild "h`wv'grchild:w`wv' Number of grandchildren R/P"

*wave 1 couple-level flag number of grandchildren
chimputation_flag ngrchild_f, gen(h`wv'fgrchild) by(h`wv'coupid)
label variable h`wv'fgrchild "h`wv'fgrchild:w`wv' Impute Flag Num of grandchildren"
label values h`wv'fgrchild fchild

***Number of living siblings***
*wave 1 respondent number of living brothers
gen r1livbro =.
missing_common dn036_, result(r1livbro) wave(`wv')
replace r1livbro = 0 if dn034_ == 5
replace r1livbro = dn036_ if inrange(dn036_,0,20)
label variable r1livbro "r1livbro:w1 r Number of living brothers"

*wave 1 respondent number of living sisters
gen r1livsis =.
missing_common dn037_, result(r1livsis) wave(`wv')
replace r1livsis = 0 if dn034_ == 5
replace r1livsis = dn037_ if inrange(dn037_,0,20)
label variable r1livsis "r1livsis:w1 r Number of living sisters"

*wave 1 respondent number of living siblings
gen r1livsib =.
replace r1livsib = r1livbro + r1livsis
missing_h r1livbro r1livsis,result(r1livsib)
label variable r1livsib "r1livsib:w1 r Number of living siblings"

*wave 1 spouse number of living brothers
gen s1livbro =.
spouse r1livbro, result(s1livbro) wave(1)
label variable s1livbro "s1livbro:w1 s Number of living brothers"

*wave 1 spouse number of living sisters
gen s1livsis =.
spouse r1livsis, result(s1livsis) wave(1)
label variable s1livsis "s1livsis:w1 s Number of living sisters"

*wave 1 spouse number of living siblings
gen s1livsib =.
spouse r1livsib, result(s1livsib) wave(1)
label variable s1livsib "s1livsib:w1 s Number of living siblings"

***Number of living parents***
*wave 1 respondent number of living parents
gen r1livpar=.
missing_h r1momliv r1dadliv,result(r1livpar)
replace r1livpar=0 if !missing(r1momliv) & !missing(r1dadliv)
replace r1livpar = r1livpar +1 if r1momliv==1 & !missing(r1dadliv)
replace r1livpar = r1livpar +1 if r1dadliv==1 & !missing(r1momliv)
label variable r1livpar "r1livpar:w1 r Number of living parents"

*wave 1 spouse number of living parents
gen s1livpar =.
spouse r1livpar, result(s1livpar) wave(1)
label variable s1livpar "s1livpar:w1 s Number of living parents"

***Number of living sons/daughters***
*wave 1 household number of living sons
recode ch005_* (-2 -1 2 = 0), prefix(m)
egen h`wv'son = rowtotal(mch005_*) if inw`wv' == 1,m 
replace h`wv'son = .m if (ch005_1==. | ch005_2==. | ch005_3==. | ch005_4==. | /// 
	ch005_5==. | ch005_6==. | ch005_7==. | ch005_8==. | ch005_9==. | ch005_10==. | ///
	ch005_11==. | ch005_12==. | ch005_13==. | ch005_14==. | ch005_15==. | ch005_16==. | ///
	ch005_17==. | ch005_18==. | ch005_19==. | ch005_20==.) & inlist(h`wv'son,.,0) & inw`wv' == 1
replace h`wv'son = .d if (ch005_1==-1 | ch005_2==-1 | ch005_3==-1 | ch005_4==-1 | /// 
	ch005_5==-1 | ch005_6==-1 | ch005_7==-1 | ch005_8==-1 | ch005_9==-1 | ch005_11==-1 | ///
	ch005_11==-1 | ch005_12==-1 | ch005_13==-1 | ch005_14==-1 | ch005_15==-1 | ch005_16==-1 | ///
	ch005_17==-1 | ch005_18==-1 | ch005_19==-1 | ch005_20==-1) & inlist(h`wv'son,.,.m,0)
replace h`wv'son = .r if (ch005_1==-2 | ch005_2==-2 | ch005_3==-2 | ch005_4==-2 | /// 
	ch005_5==-2 | ch005_6==-2 | ch005_7==-2 | ch005_8==-2 | ch005_9==-2 | ch005_11==-2 | ///
	ch005_11==-2 | ch005_12==-2 | ch005_13==-2 | ch005_14==-2 | ch005_15==-2 | ch005_16==-2 | ///
	ch005_17==-2 | ch005_18==-2 | ch005_19==-2 | ch005_20==-2) & inlist(h`wv'son,.,.m,0)

*wave 1 household number of living daughters
recode ch005_* (-2 -1 1 = 0) (2 = 1), prefix(f)
egen h`wv'dau = rowtotal(fch005_*) if inw`wv' == 1,m
replace h`wv'dau = .m if (ch005_1==. | ch005_2==. | ch005_3==. | ch005_4==. | /// 
	ch005_5==. | ch005_6==. | ch005_7==. | ch005_8==. | ch005_9==. | ch005_11==. | ///
	ch005_11==. | ch005_12==. | ch005_13==. | ch005_14==. | ch005_15==. | ch005_16==. | ///
	ch005_17==. | ch005_18==. | ch005_19==. | ch005_20==.) & inlist(h`wv'dau,.,0) & inw`wv' == 1
replace h`wv'dau = .d if (ch005_1==-1 | ch005_2==-1 | ch005_3==-1 | ch005_4==-1 | /// 
	ch005_5==-1 | ch005_6==-1 | ch005_7==-1 | ch005_8==-1 | ch005_9==-1 | ch005_11==-1 | ///
	ch005_11==-1 | ch005_12==-1 | ch005_13==-1 | ch005_14==-1 | ch005_15==-1 | ch005_16==-1 | ///
	ch005_17==-1 | ch005_18==-1 | ch005_19==-1 | ch005_20==-1) & inlist(h`wv'dau,.,.m,0)
replace h`wv'dau = .r if (ch005_1==-2 | ch005_2==-2 | ch005_3==-2 | ch005_4==-2 | /// 
	ch005_5==-2 | ch005_6==-2 | ch005_7==-2 | ch005_8==-2 | ch005_9==-2 | ch005_11==-2 | ///
	ch005_11==-2 | ch005_12==-2 | ch005_13==-2 | ch005_14==-2 | ch005_15==-2 | ch005_16==-2 | ///
	ch005_17==-2 | ch005_18==-2 | ch005_19==-2 | ch005_20==-2) & inlist(h`wv'dau,.,.m,0)
replace h`wv'son = 0 if h`wv'child == 0 | ((h`wv'child == h`wv'dau) & !mi(h`wv'dau))
replace h`wv'dau = 0 if h`wv'child == 0 | ((h`wv'child == h`wv'son) & !mi(h`wv'son))
label variable h`wv'son "h`wv'son:w`wv' Number of living sons R/P"
label variable h`wv'dau "h`wv'dau:w`wv' Number of living daughters R/P"

bysort h`wv'coupid: replace h`wv'son = h`wv'son[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'son = h`wv'son[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

bysort h`wv'coupid: replace h`wv'dau = h`wv'dau[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'dau = h`wv'dau[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

drop mch005_* fch005_*

***Any Child Co-Resides***
*wave 1 household any child co-resides
gen h`wv'coresd = .
missing_common ch007_1 ch007_2 ch007_3 ch007_4 ch007_5 ch007_6 ch007_7 ch007_8 ch007_9 ch007_10 ///
	 ch007_11 ch007_12 ch007_13 ch007_14 ch007_15 ch007_16 ch007_17 ch007_18 ch007_19 ch007_20, result(h`wv'coresd) wave(`wv')
replace h`wv'coresd = .k if nchild == 0
replace h`wv'coresd = 0 if nchild != 0 & (inrange(ch007_1,2,9) | inrange(ch007_2,2,9) | inrange(ch007_3,2,9) | inrange(ch007_4,2,9) | /// 
		inrange(ch007_5,2,9) | inrange(ch007_6,2,9) | inrange(ch007_7,2,9) | inrange(ch007_8,2,9) | inrange(ch007_9,2,9) | ///
		inrange(ch007_10,2,9) | inrange(ch007_11,2,9) | inrange(ch007_12,2,9) | inrange(ch007_13,2,9) | inrange(ch007_14,2,9) | ///
		inrange(ch007_15,2,9) | inrange(ch007_16,2,9) | inrange(ch007_17,2,9) | inrange(ch007_18,2,9) | inrange(ch007_19,2,9) | inrange(ch007_20,2,9))
replace h`wv'coresd = 1 if nchild != 0 & (ch007_1 == 1 | ch007_2 == 1 | ch007_3 == 1 | ch007_4 == 1 | ch007_5 == 1 | ch007_6 == 1 | ///
		ch007_7 == 1 | ch007_8 == 1 | ch007_9 == 1 | ch007_10 == 1 | ch007_11 == 1 | ch007_12 == 1 | ch007_13 == 1 | ///
		ch007_14 == 1 | ch007_15 == 1 | ch007_16 == 1 | ch007_17 == 1 | ch007_18 == 1 | ch007_19 == 1 | ch007_20 == 1)
label variable h`wv'coresd "h`wv'coresd:w`wv' any child coresides with R/P"
label values h`wv'coresd child

bysort h`wv'coupid: replace h`wv'coresd = h`wv'coresd[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'coresd = h`wv'coresd[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Child Lives Near***
*wave 1 household any child lives near
gen h`wv'lvnear = .
missing_common ch007_1 ch007_2 ch007_3 ch007_4 ch007_5 ch007_6 ch007_7 ch007_8 ch007_9 ch007_10 ///
	 ch007_11 ch007_12 ch007_13 ch007_14 ch007_15 ch007_16 ch007_17 ch007_18 ch007_19 ch007_20, result(h`wv'lvnear) wave(`wv')
replace h`wv'lvnear = .k if nchild == 0
replace h`wv'lvnear = 0 if nchild != 0 & (inrange(ch007_1,6,9) | inrange(ch007_2,6,9) | inrange(ch007_3,6,9) | inrange(ch007_4,6,9) | /// 
		inrange(ch007_5,6,9) | inrange(ch007_6,6,9) | inrange(ch007_7,6,9) | inrange(ch007_8,6,9) | inrange(ch007_9,6,9) | ///
		inrange(ch007_10,6,9) | inrange(ch007_11,6,9) | inrange(ch007_12,6,9) | inrange(ch007_13,6,9) | inrange(ch007_14,6,9) | ///
		inrange(ch007_15,6,9) | inrange(ch007_16,6,9) | inrange(ch007_17,6,9) | inrange(ch007_18,6,9) | inrange(ch007_19,6,9) | inrange(ch007_20,6,9))
replace h`wv'lvnear = 1 if nchild != 0 & (inrange(ch007_1,1,5) | inrange(ch007_2,1,5) | inrange(ch007_3,1,5) | inrange(ch007_4,1,5) | /// 
		inrange(ch007_5,1,5) | inrange(ch007_6,1,5) | inrange(ch007_7,1,5) | inrange(ch007_8,1,5) | inrange(ch007_9,1,5) | ///
		inrange(ch007_10,1,5) | inrange(ch007_11,1,5) | inrange(ch007_12,1,5) | inrange(ch007_13,1,5) | inrange(ch007_14,1,5) | ///
		inrange(ch007_15,1,5) | inrange(ch007_16,1,5) | inrange(ch007_17,1,5) | inrange(ch007_18,1,5) | inrange(ch007_19,1,5) | inrange(ch007_20,1,5))
label variable h`wv'lvnear "h`wv'lvnear:w`wv' any child lives within 30 min"
label values h`wv'lvnear child

bysort h`wv'coupid: replace h`wv'lvnear = h`wv'lvnear[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'lvnear = h`wv'lvnear[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Weekly Contact with Children***
*wave 1 household any weekly contact with children
gen h`wv'kcnt = .
missing_common ch014_1 ch014_2 ch014_3 ch014_4, result(h`wv'kcnt) wave(`wv')
replace h`wv'kcnt = .d if h`wv'coresd == .d
replace h`wv'kcnt = .r if h`wv'coresd == .r
replace h`wv'kcnt = .k if nchild == 0
replace h`wv'kcnt = 0 if nchild != 0 & (inrange(ch014_1,4,7) | inrange(ch014_2,4,7) | inrange(ch014_3,4,7) | inrange(ch014_4,4,7))
replace h`wv'kcnt = 1 if nchild != 0 & (inrange(ch014_1,1,3) | inrange(ch014_2,1,3) | inrange(ch014_3,1,3) | inrange(ch014_4,1,3) | h`wv'coresd == 1)
label variable h`wv'kcnt "h`wv'kcnt:w`wv' any weekly contact with children in person/phone/mail"
label values h`wv'kcnt contact

bysort h`wv'coupid: replace h`wv'kcnt = h`wv'kcnt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'kcnt = h`wv'kcnt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Frequency of Contact with Children***
*wave 1 household frequency contact with children
gen h`wv'kcntf = .
missing_common ch014_1 ch014_2 ch014_3 ch014_4, result(h`wv'kcntf) wave(`wv')
replace h`wv'kcntf = .s if fam_resp == 0
replace h`wv'kcntf = .d if h`wv'coresd == .d
replace h`wv'kcntf = .r if h`wv'coresd == .r
replace h`wv'kcntf = .k if nchild == 0
replace h`wv'kcntf = 7 if nchild != 0 & (ch014_1 == 7 | ch014_2 == 7 | ch014_3 == 7 | ch014_4 == 7)
replace h`wv'kcntf = 6 if nchild != 0 & (ch014_1 == 6 | ch014_2 == 6 | ch014_3 == 6 | ch014_4 == 6)
replace h`wv'kcntf = 5 if nchild != 0 & (ch014_1 == 5 | ch014_2 == 5 | ch014_3 == 5 | ch014_4 == 5)
replace h`wv'kcntf = 4 if nchild != 0 & (ch014_1 == 4 | ch014_2 == 4 | ch014_3 == 4 | ch014_4 == 4)
replace h`wv'kcntf = 3 if nchild != 0 & (ch014_1 == 3 | ch014_2 == 3 | ch014_3 == 3 | ch014_4 == 3)
replace h`wv'kcntf = 2 if nchild != 0 & (ch014_1 == 2 | ch014_2 == 2 | ch014_3 == 2 | ch014_4 == 2)
replace h`wv'kcntf = 1 if nchild != 0 & (h`wv'coresd == 1 | ch014_1 == 1 | ch014_2 == 1 | ch014_3 == 1 | ch014_4 == 1)
label variable h`wv'kcntf "h`wv'kcntf:w`wv' frequency contact with children in person/phone/mail"
label values h`wv'kcntf freqcont

bysort h`wv'coupid: replace h`wv'kcntf = h`wv'kcntf[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'kcntf = h`wv'kcntf[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Weekly Contact with Parents***
*wave 1 respondent any weekly contact with parents
gen r`wv'pcnt = .
missing_common dn032_1 dn032_2 dn030_1 dn030_2, result(r`wv'pcnt) wave(`wv')
replace r`wv'pcnt = .n if r`wv'livpar == 0
replace r`wv'pcnt = 0 if inrange(dn032_1,4,7) | inrange(dn032_2,4,7)
replace r`wv'pcnt = 1 if inrange(dn032_1,1,3) | inrange(dn032_2,1,3) | dn030_1 == 1 | dn030_2 == 1 
label variable r`wv'pcnt "r`wv'pcnt:w`wv' r any weekly contact with parents in person/phone/mail"
label values r`wv'pcnt contact

*wave 1 spouse any weekly contact with parents
gen s`wv'pcnt = .
spouse r`wv'pcnt, result(s`wv'pcnt) wave(`wv')
label variable s`wv'pcnt "s`wv'pcnt:w`wv' s any weekly contact with parents in person/phone/mail"
label values s`wv'pcnt contact

***Frequency of Contact with Parents***
*wave 1 respondent frequency contact with parents
gen r`wv'pcntf = .
missing_common dn032_1 dn032_2 dn030_1 dn030_2, result(r`wv'pcntf) wave(`wv')
replace r`wv'pcntf = .n if r`wv'livpar == 0
replace r`wv'pcntf = 7 if dn032_1 == 7 | dn032_2 == 7
replace r`wv'pcntf = 6 if dn032_1 == 6 | dn032_2 == 6
replace r`wv'pcntf = 5 if dn032_1 == 5 | dn032_2 == 5
replace r`wv'pcntf = 4 if dn032_1 == 4 | dn032_2 == 4
replace r`wv'pcntf = 3 if dn032_1 == 3 | dn032_2 == 3
replace r`wv'pcntf = 2 if dn032_1 == 2 | dn032_2 == 2
replace r`wv'pcntf = 1 if dn032_1 == 1 | dn032_2 == 1 | dn030_1 == 1 | dn030_2 == 1
label variable r`wv'pcntf "r`wv'pcntf:w`wv' r frequency contact with parents in person/phone/mail"
label values r`wv'pcntf freqcont

*wave 1 spouse frequency contact with parents
gen s`wv'pcntf = .
spouse r`wv'pcntf, result(s`wv'pcntf) wave(`wv')
label variable s`wv'pcntf "s`wv'pcntf:w`wv' s frequency contact with parents in person/phone/mail"
label values s`wv'pcntf freqcont

***Received Any Informal Care***
*wave 1 household received any informal care
gen hout = .
missing_common sp002_, result(hout) wave(`wv')
replace hout = 0 if sp002_ == 5
replace hout = 1 if sp002_ == 1
bysort h`wv'coupid: replace hout = hout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace hout = hout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rin = .
missing_common sp020_ hhsize ph048dno, result(rin) wave(`wv')
replace rin = 0 if sp020_ == 5 | hhsize == 1 | ph048dno == 1
replace rin = 1 if sp020_ == 1

gen spin = .
spouse rin, result(spin) wave(`wv')

gen h`wv'raany = .
replace h`wv'raany = .b if mi(spin) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .m if (hout == .m | rin == .m | spin == .m | r`wv'mstat == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .m if (hout == .m | rin == .m | r`wv'mstat == .m) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = .r if (hout == .r | rin == .r | spin == .r | r`wv'mstat == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .r if (hout == .r | rin == .r | r`wv'mstat == .r) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = .d if (hout == .d | rin == .d | spin == .d | r`wv'mstat == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .d if (hout == .d | rin == .d | r`wv'mstat == .d) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = 0 if (hout == 0 & rin == 0 & spin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = 0 if (hout == 0 & rin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = 1 if (hout == 1 | rin == 1 | spin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = 1 if (hout == 1 | rin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
label variable h`wv'raany "h`wv'raany:w`wv' received any informal care"
label values h`wv'raany carer

drop hout rin spin

***Received Informal Care from Children***
*wave 1 household received informal care from children
gen chout = .
missing_common sp002_ sp003_1 sp003_2 sp003_3 nchild, result(chout) wave(`wv')
replace chout = .k if nchild == 0
replace chout = 0 if nchild != 0 & (sp002_ == 5 | (sp002_ == 1 & ((inrange(sp003_1,1,9) | inrange(sp003_1,22,33)) | /// 
		(inrange(sp003_2,1,9) | inrange(sp003_2,22,33)) | (inrange(sp003_3,1,9) | inrange(sp003_3,22,33)))))
replace chout = 1 if nchild != 0 & (sp002_ == 1 & (inrange(sp003_1,10,21) | inrange(sp003_2,10,21) | inrange(sp003_3,10,21)))
bysort h`wv'coupid: replace chout = chout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace chout = chout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen crin = .
missing_common sp020_ sp021d10 sp021d11 sp021d12 sp021d13 sp021d14 sp021d15 sp021d16 sp021d17 sp021d18 sp021d19 ///
		sp021d20 sp021d21 hhsize ph048dno nchild, result(crin) wave(`wv')
replace crin = .k if nchild == 0
replace crin = 0 if hhsize == 1 | ph048dno == 1 | (nchild != 0 & (sp020_ == 5 | (sp020_ == 1 & (sp021d10 != 1 | sp021d11 != 1 | sp021d12 != 1 | ///
		sp021d13 != 1 | sp021d14 != 1 | sp021d15 != 1 | sp021d16 != 1 | sp021d17 != 1 | sp021d18 != 1 | sp021d19 != 1 | sp021d20 != 1 | sp021d21 != 1))))
replace crin = 1 if nchild != 0 & sp020_ == 1 & (sp021d10 == 1 | sp021d11 == 1 | sp021d12 == 1 | sp021d13 == 1 | ///
		sp021d14 == 1 | sp021d15 == 1 | sp021d16 == 1 | sp021d17 == 1 | sp021d18 == 1 | sp021d19 == 1 | sp021d20 == 1 | sp021d21 == 1)

gen cspin = .
spouse crin, result(cspin) wave (`wv')

gen h`wv'rccare = .
replace h`wv'rccare = .b if mi(cspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .m if (chout == .m | crin == .m | cspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .m if (chout == .m | crin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .r if (chout == .r | crin == .r | cspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .r if (chout == .r | crin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .d if (chout == .d | crin == .d | cspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .d if (chout == .d | crin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .k if (chout == .k | crin == .k | cspin == .k) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .k if (chout == .k | crin == .k) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = 0 if (chout == 0 & crin == 0 & cspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = 0 if (chout == 0 & crin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = 1 if (chout == 1 | crin == 1 | cspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = 1 if (chout == 1 | crin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
label variable h`wv'rccare "h`wv'rccare:w`wv' received informal care from children"
label values h`wv'rccare carer

drop chout crin cspin

***Received Informal Care from Relatives***
*wave 1 household received informal care from relatives
gen rhout = .
missing_common sp002_ sp003_1 sp003_2 sp003_3, result(rhout) wave(`wv')
replace rhout = 0 if sp002_ == 5 | (sp002_ == 1 & ((inrange(sp003_1,10,21) | inrange(sp003_1,29,33)) | /// 
		(inrange(sp003_2,10,21) | inrange(sp003_2,29,33)) | (inrange(sp003_3,10,21) | inrange(sp003_3,29,33))))
replace rhout = 1 if sp002_ == 1 & (inrange(sp003_1,1,9) | inrange(sp003_1,22,28) | inrange(sp003_2,1,9) | ///
		inrange(sp003_2,22,28) | inrange(sp003_3,1,9) | inrange(sp003_3,22,28))
bysort h`wv'coupid: replace rhout = rhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace rhout = rhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rrin = .
missing_common sp020_ sp021d1 sp021d2 sp021d3 sp021d4 sp021d5 sp021d6 sp021d7 sp021d8 sp021d9 sp021d22 sp021d23 sp021d24 sp021d25 ///
		sp021d26 sp021d27 sp021d28 hhsize ph048dno, result(rrin) wave(`wv')
replace rrin = 0 if hhsize == 1 | ph048dno == 1 | (sp020_ == 5 | (sp020_ == 1 & (sp021d1 != 1 | sp021d2 != 1 | sp021d3 != 1 | sp021d4 != 1 | ///
		sp021d5 != 1 | sp021d6 != 1 | sp021d7 != 1 | sp021d8 != 1 | sp021d9 != 1 | sp021d22 != 1 | sp021d23 != 1 | sp021d24 != 1 | ///
		sp021d25 != 1 | sp021d26 != 1 | sp021d27 != 1 | sp021d28 != 1)))
replace rrin = 1 if sp020_ == 1 & (sp021d1 == 1 | sp021d2 == 1 | sp021d3 == 1 | sp021d4 == 1 | ///
		sp021d5 == 1 | sp021d6 == 1 | sp021d7 == 1 | sp021d8 == 1 | sp021d9 == 1 | sp021d22 == 1 | sp021d23 == 1 | sp021d24 == 1 | ///
		sp021d25 == 1 | sp021d26 == 1 | sp021d27 == 1 | sp021d28 == 1)

gen rspin = .
spouse rrin, result(rspin) wave (`wv')

gen h`wv'rrcare = .
replace h`wv'rrcare = .b if mi(rspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .m if (rhout == .m | rrin == .m | rspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .m if (rhout == .m | rrin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = .r if (rhout == .r | rrin == .r | rspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .r if (rhout == .r | rrin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = .d if (rhout == .d | rrin == .d | rspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .d if (rhout == .d | rrin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = 0 if (rhout == 0 & rrin == 0 & rspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = 0 if (rhout == 0 & rrin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = 1 if (rhout == 1 | rrin == 1 | rspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = 1 if (rhout == 1 | rrin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
label variable h`wv'rrcare "h`wv'rrcare:w`wv' received informal care from relatives"
label values h`wv'rrcare carer

drop rhout rrin rspin

***Received Informal Care from non-relatives***
*wave 1 household received informal care from non-relatives
gen fhout = .
missing_common sp002_ sp003_1 sp003_2 sp003_3, result(fhout) wave(`wv')
replace fhout = 0 if sp002_ == 5 | (sp002_ == 1 & (inrange(sp003_1,1,28) | inrange(sp003_2,1,28) | inrange(sp003_3,1,28)))
replace fhout = 1 if sp002_ == 1 & (inrange(sp003_1,29,33) | inrange(sp003_2,29,33) | inrange(sp003_3,29,33))
bysort h`wv'coupid: replace fhout = fhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace fhout = fhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen frin = .
missing_common sp020_ sp021d29 sp021d30 sp021d31 sp021d32 sp021d33 hhsize ph048dno, result(frin) wave(`wv')
replace frin = 0 if hhsize == 1 | ph048dno == 1 | (sp020_ == 5 | (sp020_ == 1 & (sp021d29 != 1 | sp021d30 != 1 | sp021d31 != 1 | sp021d32 != 1 | ///
		sp021d33 != 1)))
replace frin = 1 if sp020_ == 1 & (sp021d29 == 1 | sp021d30 == 1 | sp021d31 == 1 | sp021d32 == 1 | ///
		sp021d33 == 1)

gen fspin = .
spouse frin, result(fspin) wave (`wv')

gen h`wv'rfcare = .
replace h`wv'rfcare = .b if mi(fspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .m if (fhout == .m | frin == .m | fspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .m if (fhout == .m | frin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = .r if (fhout == .r | frin == .r | fspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .r if (fhout == .r | frin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = .d if (fhout == .d | frin == .d | fspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .d if (fhout == .d | frin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = 0 if (fhout == 0 & frin == 0 & fspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = 0 if (fhout == 0 & frin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = 1 if (fhout == 1 | frin == 1 | fspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = 1 if (fhout == 1 | frin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
label variable h`wv'rfcare "h`wv'rfcare:w`wv' received informal care from non-relatives"
label values h`wv'rfcare carer

drop fhout frin fspin

***Gave Any Informal Care***
*wave 1 respondent gave any informal care 
gen rout = .
missing_common sp008_, result(rout) wave(`wv')
replace rout = 0 if sp008_ == 5
replace rout = 1 if sp008_ == 1

gen spout = .
spouse rout, result(spout) wave(`wv')

gen rin = .
missing_common sp018_ hhsize , result(rin) wave(`wv')
replace rin = 0 if sp018_ == 5 | hhsize == 1
replace rin = 1 if sp018_ == 1

gen spin = .
spouse rin, result(spin) wave(`wv')

gen h`wv'gcaany = .
replace h`wv'gcaany = .b if (mi(spout) | mi(spin)) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .m if (rout == .m | spout == .m | rin == .m | spin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .m if (rout == .m | rin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = .r if (rout == .r | spout == .r | rin == .r | spin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .r if (rout == .r | rin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = .d if (rout == .d | spout == .d | rin == .d | spin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .d if (rout == .d | rin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = 0 if (rout == 0 & spout == 0 & rin == 0 & spin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = 0 if (rout == 0 & rin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = 1 if (rout == 1 | spout == 1 | rin == 1 | spin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = 1 if (rout == 1 | rin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
label variable h`wv'gcaany "h`wv'gcaany:w`wv' gave any informal care"
label values h`wv'gcaany careg

drop rout spout rin spin

***Gave Informal Care to Children***
*wave 1 respondent gave informal care to children
gen crout = .
missing_common sp008_ sp009_1 sp009_2 sp009_3 nchild, result(crout) wave(`wv')
replace crout = .k if nchild == 0
replace crout = 0 if nchild != 0 & (sp008_ == 5 | (sp008_ == 1 & ((inrange(sp009_1,1,9) | /// 
		inrange(sp009_1,22,33)) | (inrange(sp009_2,1,9) | inrange(sp009_2,22,33)) | ///
		(inrange(sp009_3,1,9) | inrange(sp009_3,22,33)))))
replace crout = 1 if nchild != 0 & (sp008_ == 1 & (inrange(sp009_1,10,21) | inrange(sp009_2,10,21) | inrange(sp009_3,10,21)))

gen cspout = .
spouse crout, result(cspout) wave(`wv')

gen crin = .
missing_common sp018_ sp019d10 sp019d11 sp019d12 sp019d13 sp019d14 sp019d15 sp019d16 sp019d17 sp019d18 sp019d19 ///
		sp019d20 sp019d21 hhsize ph048dno nchild, result(crin) wave(`wv')
replace crin = .k if nchild == 0
replace crin = 0 if hhsize == 1 | ph048dno == 1 | (nchild != 0 & (sp018_ == 5 | (sp018_ == 1 & (sp019d10 != 1 | sp019d11 != 1 | sp019d12 != 1 | ///
		sp019d13 != 1 | sp019d14 != 1 | sp019d15 != 1 | sp019d16 != 1 | sp019d17 != 1 | sp019d18 != 1 | sp019d19 != 1 | sp019d20 != 1 | sp019d21 != 1))))
replace crin = 1 if nchild != 0 & sp018_ == 1 & (sp019d10 == 1 | sp019d11 == 1 | sp019d12 == 1 | sp019d13 == 1 | ///
		sp019d14 == 1 | sp019d15 == 1 | sp019d16 == 1 | sp019d17 == 1 | sp019d18 == 1 | sp019d19 == 1 | sp019d20 == 1 | sp019d21 == 1)

gen cspin = .
spouse crin, result(cspin) wave(`wv')

gen h`wv'gccare = .
replace h`wv'gccare = .b if (mi(cspout) | mi(cspin)) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .m if (crout == .m | cspout == .m | crin == .m | cspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .m if (crout == .m | crin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .r if (crout == .r | cspout == .r | crin == .r | cspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .r if (crout == .r | crin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .d if (crout == .d | cspout == .d | crin == .d | cspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .d if (crout == .d | crin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .k if (crout == .k | cspout == .k | crin == .k | cspin == .k) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .k if (crout == .k | crin == .k) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = 0 if (crout == 0 & cspout == 0 & crin == 0 & cspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = 0 if (crout == 0 & crin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = 1 if (crout == 1 | cspout == 1 | crin == 1 | cspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = 1 if (crout == 1 | crin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
label variable h`wv'gccare "h`wv'gccare:w`wv' gave informal care to children"
label values h`wv'gccare careg

drop crout cspout crin cspin

***Gave Informal Care to Relatives***
*wave 1 respondent gave informal care to relatives
gen rrout = .
missing_common sp008_ sp009_1 sp009_2 sp009_3, result(rrout) wave(`wv')
replace rrout = 0 if sp008_ == 5 | (sp008_ == 1 & (inrange(sp009_1,10,21) | inrange(sp009_1,29,33) | inrange(sp009_2,10,21) | ///
		inrange(sp009_2,29,33) | inrange(sp009_3,10,21) | inrange(sp009_3,29,33)))
replace rrout = 1 if sp008_ == 1 & (inrange(sp009_1,1,9) | inrange(sp009_1,22,28) | inrange(sp009_2,1,9) | inrange(sp009_2,22,28) | ///
		inrange(sp009_3,1,9) | inrange(sp009_3,22,28))
list sp008_ sp009_1 sp009_2 sp009_3	if rrout == . & inw1==1

gen rspout = .
spouse rrout, result(rspout) wave(`wv')

gen rrin = .
missing_common sp018_ sp019d1 sp019d2 sp019d3 sp019d4 sp019d5 sp019d6 sp019d7 sp019d8 sp019d9 ///
		sp019d23 sp019d24 sp019d25 sp019d26 sp019d27 sp019d28 hhsize ph048dno, result(rrin) wave(`wv')
replace rrin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & (sp019d1 != 1 | sp019d2 != 1 | sp019d3 != 1 | ///
		sp019d4 != 1 | sp019d5 != 1 | sp019d6 != 1 | sp019d7 != 1 | sp019d8 != 1 | sp019d9 != 1 | sp019d23 != 1 | sp019d24 != 1 | ///
		sp019d25 != 1 | sp019d26 != 1 | sp019d27 != 1 | sp019d28 != 1)))
replace rrin = 1 if sp018_ == 1 & (sp019d1 == 1 | sp019d2 == 1 | sp019d3 == 1 | sp019d4 == 1 | sp019d5 == 1 | sp019d6 == 1 | sp019d7 == 1 | ///
		sp019d8 == 1 | sp019d9 == 1 | sp019d23 == 1 | sp019d24 == 1 | sp019d25 == 1 | sp019d26 == 1 | sp019d27 == 1 | sp019d28 == 1)

gen rspin = .
spouse rrin, result(rspin) wave(`wv')

gen h`wv'grcare = .
replace h`wv'grcare = .b if (mi(rspout) | mi(rspin)) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .m if (rrout == .m | rspout == .m | rrin == .m | rspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .m if (rrout == .m | rrin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = .r if (rrout == .r | rspout == .r | rrin == .r | rspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .r if (rrout == .r | rrin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = .d if (rrout == .d | rspout == .d | rrin == .d | rspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .d if (rrout == .d | rrin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = 0 if (rrout == 0 & rspout == 0 & rrin == 0 & rspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = 0 if (rrout == 0 & rrin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = 1 if (rrout == 1 | rspout == 1 | rrin == 1 | rspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = 1 if (rrout == 1 | rrin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
label variable h`wv'grcare "h`wv'grcare:w`wv' gave informal care to relatives"
label values h`wv'grcare careg

drop rrout rspout rrin rspin

***Gave Informal Care to non-relatives***
*wave 1 respondent gave informal care to non-relatives
gen frout = .
missing_common sp008_ sp009_1 sp009_2 sp009_3, result(frout) wave(`wv')
replace frout = 0 if sp008_ == 5 | (sp008_ == 1 & (inrange(sp009_1,1,28) | inrange(sp009_2,1,28) | inrange(sp009_3,1,28)))
replace frout = 1 if sp008_ == 1 & (inrange(sp009_1,29,33) | inrange(sp009_2,29,33) | inrange(sp009_3,29,33))

gen fspout = .
spouse frout, result(fspout) wave(`wv')

gen frin = .
missing_common sp018_ sp019d29 sp019d30 sp019d31 sp019d32 sp019d33 hhsize ph048dno, result(frin) wave(`wv')
replace frin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & (sp019d29 != 1 | sp019d30 != 1 | sp019d31 != 1 | ///
		sp019d32 != 1 | sp019d33 != 1)))
replace frin = 1 if sp018_ == 1 & (sp019d29 == 1 | sp019d30 == 1 | sp019d31 == 1 | sp019d32 == 1 | sp019d33 == 1)

gen fspin = .
spouse frin, result(fspin) wave(`wv')

gen h`wv'gfcare = .
replace h`wv'gfcare = .b if (mi(fspout) | mi(fspin)) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .m if (frout == .m | fspout == .m | frin == .m | fspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .m if (frout == .m | frin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = .r if (frout == .r | fspout == .r | frin == .r | fspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .r if (frout == .r | frin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = .d if (frout == .d | fspout == .d | frin == .d | fspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .d if (frout == .d | frin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = 0 if (frout == 0 & fspout == 0 & frin == 0 & fspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = 0 if (frout == 0 & frin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = 1 if (frout == 1 | fspout == 1 | frin == 1 | fspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = 1 if (frout == 1 | frin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
label variable h`wv'gfcare "h`wv'gfcare:w`wv' gave informal care to non-relatives"
label values h`wv'gfcare careg

drop frout fspout frin fspin

***Gave Informal Care to Grandchildren***
*wave 1 respondent gave care to grandchildren
gen krout = .
missing_common sp014_ ngrchild, result(krout) wave(`wv')
replace krout = .g if ngrchild == 0
replace krout = 0 if ngrchild != 0 & sp014_ == 5
replace krout = 1 if ngrchild != 0 & sp014_ == 1

gen kspout = .
spouse krout, result(kspout) wave(`wv')

gen krin = .
missing_common sp018_ sp019d22 hhsize ph048dno, result(krin) wave(`wv')
replace krin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & sp019d22 != 1))
replace krin = 1 if sp018_ == 1 & sp019d22 == 1

gen kspin = .
spouse krin, result(kspin) wave(`wv')

gen h`wv'gkcare = .
replace h`wv'gkcare = .b if (mi(kspout) | mi(kspin)) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .m if (krout == .m | kspout == .m | krin == .m | kspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .m if (krout == .m | krin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .r if (krout == .r | kspout == .r | krin == .r | kspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .r if (krout == .r | krin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .d if (krout == .d | kspout == .d | krin == .d | kspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .d if (krout == .d | krin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .g if (krout == .g | kspout == .g | krin == .g | kspin == .g) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .g if (krout == .g | krin == .g) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = 0 if (krout == 0 & kspout == 0 & krin == 0 & kspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = 0 if (krout == 0 & krin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = 1 if (krout == 1 | kspout == 1 | krin == 1 | kspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = 1 if (krout == 1 | krin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
label variable h`wv'gkcare "h`wv'gkcare:w`wv' gave care to grandchildren"
label values h`wv'gkcare careg

drop krout kspout krin kspin

***Receives Help with ADLs***
*wave 1 household receives help with ADLs
gen hadl = .
missing_common sp002_ sp004d1_1 sp004d1_2 sp004d1_3 sp004d2_1 sp004d2_2 sp004d2_3 ///
		sp004d3_1 sp004d3_2 sp004d3_3, result(hadl) wave(`wv')
replace hadl = 0 if sp002_ == 5 | (sp002_ == 1 & (inlist(sp004d1_1,2,3) | inlist(sp004d1_2,2,3) | inlist(sp004d1_3,2,3) | ///
		 inlist(sp004d2_1,2,3) | inlist(sp004d2_2,2,3) | inlist(sp004d2_3,2,3) | inlist(sp004d3_1,2,3) | inlist(sp004d3_2,2,3) | inlist(sp004d3_3,2,3))) 
replace hadl = 1 if sp002 == 1 & (sp004d1_1 == 1 | sp004d1_2 == 1 | sp004d1_3 == 1 | sp004d2_1 == 1 | sp004d2_2 == 1 | ///
			sp004d2_3 == 1 | sp004d3_1 == 1 | sp004d3_2 == 1 | sp004d3_3 == 1) 
bysort h`wv'coupid: replace hadl = hadl[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace hadl = hadl[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen radl = .
missing_common sp020_ hhsize ph048dno, result(radl) wave(`wv')
replace radl = 0 if sp020_ == 5 | hhsize == 1 | ph048dno == 1
replace radl = 1 if sp020_ == 1

gen spadl = .
spouse radl, result(spadl) wave(`wv')

gen h`wv'adlcare = .
replace h`wv'adlcare = .b if mi(spadl) & inlist(r`wv'mstat,1,3)
replace h`wv'adlcare = .m if (hadl == .m | radl == .m | spadl == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'adlcare = .m if (hadl == .m | radl == .m) & (!inlist(r`wv'mstat,1,3) | inlist(spadl,.u,.v))
replace h`wv'adlcare = .d if (hadl == .d | radl == .d | spadl == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'adlcare = .d if (hadl == .d | radl == .d) & (!inlist(r`wv'mstat,1,3) | inlist(spadl,.u,.v))
replace h`wv'adlcare = .r if (hadl == .r | radl == .r | spadl == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'adlcare = .r if (hadl == .r | radl == .r) & (!inlist(r`wv'mstat,1,3) | inlist(spadl,.u,.v))
replace h`wv'adlcare = 0 if (hadl == 0 & radl == 0 & spadl == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'adlcare = 0 if (hadl == 0 & radl == 0) & (!inlist(r`wv'mstat,1,3) | inlist(spadl,.u,.v))
replace h`wv'adlcare = 1 if (hadl == 1 | radl == 1 | spadl == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'adlcare = 1 if (hadl == 1 | radl == 1) & (!inlist(r`wv'mstat,1,3) | inlist(spadl,.u,.v))
label variable h`wv'adlcare "h`wv'adlcare:w`wv' received help with ADLs"
label values h`wv'adlcare carer

drop hadl radl spadl

***Any Transfer to Children/Grandchildren***
*wave 1 household any transfer to children/grandchildren
gen h`wv'tcany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'tcany) wave(`wv')
replace h`wv'tcany = 0 if ft002_ == 5 | (ft002_ == 1 & ((inrange(ft003_1,1,9) | inrange(ft003_1,23,33) | inrange(ft003_2,1,9) | /// 
		inrange(ft003_2,23,33) | inrange(ft003_3,1,9) | inrange(ft003_3,23,33))))
replace h`wv'tcany = 1 if ft002_ == 1 & (inrange(ft003_1,10,22) | inrange(ft003_2,10,22) | inrange(ft003_3,10,22))
label variable h`wv'tcany "h`wv'tcany:w`wv' any transfer to children/grandchildren"
label values h`wv'tcany trans

bysort h`wv'coupid: replace h`wv'tcany = h`wv'tcany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tcany = h`wv'tcany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer to Children/Grandchildren***
*wave 1 household amount transfer to children/grandchildren
gen kamt1 = .
replace kamt1 = 0 if h`wv'tcany == 0 | (mi(ft003_1) & inw`wv' == 1) | inrange(ft003_1,1,9) | inrange(ft003_1,23,33)
replace kamt1 = ft004e_1 if inrange(ft003_1,10,22) & inrange(ft004e_1,0,1000000)

gen kamt2 = .
replace kamt2 = 0 if h`wv'tcany == 0 | (mi(ft003_2) & inw`wv' == 1) | inrange(ft003_2,1,9) | inrange(ft003_2,23,33)
replace kamt2 = ft004e_2 if inrange(ft003_2,10,22) & inrange(ft004e_2,0,1000000)

gen kamt3 = 0
replace kamt3 = 0 if h`wv'tcany == 0 | (mi(ft003_3) & inw`wv' == 1) | inrange(ft003_3,1,9) | inrange(ft003_3,23,33)
replace kamt3 = ft004e_3 if inrange(ft003_3,10,22) & inrange(ft004e_3,0,1000000)

gen h`wv'tcamt = .
missing_financial ft004e_1 ft004e_1 ft004e_3, result(h`wv'tcamt)
missing_h h`wv'tcany, result(h`wv'tcamt)
replace h`wv'tcamt = kamt1 + kamt2 + kamt3 if !mi(kamt1) & !mi(kamt2) & !mi(kamt3)
label variable h`wv'tcamt "h`wv'tcamt:w`wv' amount transfer to children/grandchildren"

bysort h`wv'coupid: replace h`wv'tcamt = h`wv'tcamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tcamt = h`wv'tcamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop kamt1 kamt2 kamt3

***Any Transfers to Parents***
*wave 1 household any transfers to parents
gen h`wv'tpany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'tpany) wave(`wv')
replace h`wv'tpany = 0 if ft002_ == 5 | (ft002_ == 1 & (ft003_1 == 1 | inrange(ft003_1,8,33) | ///
		ft003_2 == 1 | inrange(ft003_2,8,33) | ft003_3 == 1 | inrange(ft003_3,8,33)))
replace h`wv'tpany = 1 if ft002_ == 1 & (inrange(ft003_1,2,7) | inrange(ft003_2,2,7) | inrange(ft003_3,2,7))
label variable h`wv'tpany "h`wv'tpany:w`wv' any transfer to parents"
label values h`wv'tpany trans

bysort h`wv'coupid: replace h`wv'tpany = h`wv'tpany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tpany = h`wv'tpany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer to Parents***
*wave 1 household amount transfer to parents
gen pamt1 = .
replace pamt1 = 0 if h`wv'tpany == 0 | (mi(ft003_1) & inw`wv' == 1) | ft003_1 == 1 | inrange(ft003_1,8,33) 
replace pamt1 = ft004e_1 if inrange(ft003_1,2,7) & inrange(ft004e_1,0,1000000)

gen pamt2 = .
replace pamt2 = 0 if h`wv'tpany == 0 | (mi(ft003_2) & inw`wv' == 1) | ft003_2 == 1 | inrange(ft003_2,8,33) 
replace pamt2 = ft004e_2 if inrange(ft003_2,2,7) & inrange(ft004e_2,0,1000000)

gen pamt3 = .
replace pamt3 = 0 if h`wv'tpany == 0 | (mi(ft003_3) & inw`wv' == 1) | ft003_3 == 1 | inrange(ft003_3,8,33) 
replace pamt3 = ft004e_3 if inrange(ft003_3,2,7) & inrange(ft004e_3,0,1000000)

gen h`wv'tpamt = .
missing_financial ft004e_1 ft004e_2 ft004e_3 , result(h`wv'tpamt)
missing_h h`wv'tpany, result(h`wv'tpamt)
replace h`wv'tpamt = pamt1 + pamt2 + pamt3 if !mi(pamt1) & !mi(pamt2) & !mi(pamt3)
label variable h`wv'tpamt "h`wv'tpamt:w`wv' amount transfer to parents"

bysort h`wv'coupid: replace h`wv'tpamt = h`wv'tpamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tpamt = h`wv'tpamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop pamt1 pamt2 pamt3 

***Any Transfers to Other Relatives***
*wave 1 household any transfers to other relatives
gen h`wv'toany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'toany) wave(`wv')
replace h`wv'toany = 0 if ft002_ == 5 | (ft002_ == 1 & (inrange(ft003_1,2,7) | inrange(ft003_1,10,22) | inrange(ft003_1,29,33) | /// 
		inrange(ft003_2,2,7) | inrange(ft003_2,10,22) | inrange(ft003_2,29,33) | inrange(ft003_3,2,7) | inrange(ft003_3,10,22) | inrange(ft003_3,29,33)))
replace h`wv'toany = 1 if ft002_ == 1 & (inlist(ft003_1,1,8,9) | inrange(ft003_1,23,28) | inlist(ft003_2,1,8,9) | ///
		inrange(ft003_2,23,28) | inlist(ft003_3,1,8,9) | inrange(ft003_3,23,28))
label variable h`wv'toany "h`wv'toany:w`wv' any transfer to other relatives"
label values h`wv'toany trans

bysort h`wv'coupid: replace h`wv'toany = h`wv'toany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'toany = h`wv'toany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer to Other Relatives***
*wave 1 household amount transfer to other relatives
gen oamt1 = .
replace oamt1 = 0 if h`wv'toany == 0 | (mi(ft003_1) & inw`wv' == 1) | inrange(ft003_1,2,7) | inrange(ft003_1,10,22) | inrange(ft003_1,29,33)
replace oamt1 = ft004e_1 if (inlist(ft003_1,1,8,9) | inrange(ft003_1,23,28)) & inrange(ft004e_1,0,1000000)

gen oamt2 = .
replace oamt2 = 0 if h`wv'toany == 0 | (mi(ft003_2) & inw`wv' == 1) | inrange(ft003_2,2,7) | inrange(ft003_2,10,22) | inrange(ft003_2,29,33)
replace oamt2 = ft004e_2 if (inlist(ft003_2,1,8,9) | inrange(ft003_2,23,28)) & inrange(ft004e_2,0,1000000)

gen oamt3 = .
replace oamt3 = 0 if h`wv'toany == 0 | (mi(ft003_3) & inw`wv' == 1) | inrange(ft003_3,2,7) | inrange(ft003_3,10,22) | inrange(ft003_3,29,33)
replace oamt3 = ft004e_3 if (inlist(ft003_3,1,8,9) | inrange(ft003_3,23,28)) & inrange(ft004e_3,0,1000000)

gen h`wv'toamt = .
missing_financial ft004e_1 ft004e_2 ft004e_3, result(h`wv'toamt)
missing_h h`wv'toany, result(h`wv'toamt)
replace h`wv'toamt = oamt1 + oamt2 + oamt3 if !mi(oamt1) & !mi(oamt2) & !mi(oamt3)
label variable h`wv'toamt "h`wv'toamt:w`wv' amount transfer to other relatives"

bysort h`wv'coupid: replace h`wv'toamt = h`wv'toamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'toamt = h`wv'toamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop oamt1 oamt2 oamt3

***Any Transfers to Other Non-Relatives***
*wave 1 household any transfers to other non-relatives
gen h`wv'tfany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'tfany) wave(`wv')
replace h`wv'tfany = 0 if ft002_ == 5 | (ft002_ == 1 & (inrange(ft003_1,1,28) | inrange(ft003_2,1,28) | inrange(ft003_3,1,28)))
replace h`wv'tfany = 1 if ft002_ == 1 & (inrange(ft003_1,29,33) | inrange(ft003_2,29,33) | inrange(ft003_3,29,33))
label variable h`wv'tfany "h`wv'tfany:w`wv' any transfer to other non-relatives"
label values h`wv'tfany trans

bysort h`wv'coupid: replace h`wv'tfany = h`wv'tfany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tfany = h`wv'tfany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer to Other Non-Relatives***
*wave 1 household amount transfer to other non-relatives
gen famt1 = .
replace famt1 = 0 if h`wv'tfany == 0 | (mi(ft003_1) & inw`wv' == 1) | inrange(ft003_1,1,28) 
replace famt1 = ft004e_1 if inrange(ft003_1,29,33) & inrange(ft004e_1,0,1000000)

gen famt2 = .
replace famt2 = 0 if h`wv'tfany == 0 | (mi(ft003_2) & inw`wv' == 1) | inrange(ft003_2,1,28) 
replace famt2 = ft004e_2 if inrange(ft003_2,29,33) & inrange(ft004e_2,0,1000000)

gen famt3 = .
replace famt3 = 0 if h`wv'tfany == 0 | (mi(ft003_3) & inw`wv' == 1) | inrange(ft003_3,1,28) 
replace famt3 = ft004e_3 if inrange(ft003_3,29,33) & inrange(ft004e_3,0,1000000)

gen h`wv'tfamt = .
missing_financial ft004e_1 ft004e_2 ft004e_3 , result(h`wv'tfamt)
missing_h h`wv'tfany, result(h`wv'tfamt)
replace h`wv'tfamt = famt1 + famt2 + famt3 if !mi(famt1) & !mi(famt2) & !mi(famt3)
label variable h`wv'tfamt "h`wv'tfamt:w`wv' amount transfer to other non-relatives"

bysort h`wv'coupid: replace h`wv'tfamt = h`wv'tfamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tfamt = h`wv'tfamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop famt1 famt2 famt3

***Any Transfers from Children/Grandchildren***
*wave 1 household any transfers from children/grandchildren
gen h`wv'fcany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'fcany) wave(`wv')
replace h`wv'fcany = 0 if ft009_ == 5 | (ft009_ == 1 & (inrange(ft010_1,1,9) | inrange(ft010_1,23,33) | inrange(ft010_2,1,9) | ///
		 inrange(ft010_2,23,33) | inrange(ft010_3,1,9) | inrange(ft010_3,23,33)))
replace h`wv'fcany = 1 if ft009_ == 1 & (inrange(ft010_1,10,22) | inrange(ft010_2,10,22) | inrange(ft010_3,10,22))
label variable h`wv'fcany "h`wv'fcany:w`wv' any transfers from children/grandchildren"
label values h`wv'fcany trans

bysort h`wv'coupid: replace h`wv'fcany = h`wv'fcany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fcany = h`wv'fcany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer from Children/Grandchildren***
*wave 1 household amount transfers from children/grandchildren
gen chamt1 = .
replace chamt1 = 0 if h`wv'fcany == 0 | (mi(ft010_1) & inw`wv' == 1) | inrange(ft010_1,1,9) | inrange(ft010_1,23,33)
replace chamt1 = ft011e_1 if inrange(ft010_1,10,22) & inrange(ft011e_1,0,1000000)

gen chamt2 = .
replace chamt2 = 0 if h`wv'fcany == 0 | (mi(ft010_2) & inw`wv' == 1) | inrange(ft010_2,1,9) | inrange(ft010_2,23,33)
replace chamt2 = ft011e_2 if inrange(ft010_2,10,22) & inrange(ft011e_2,0,1000000)

gen chamt3 = .
replace chamt3 = 0 if h`wv'fcany == 0 | (mi(ft010_3) & inw`wv' == 1) | inrange(ft010_3,1,9) | inrange(ft010_3,23,33)
replace chamt3 = ft011e_3 if inrange(ft010_3,10,22) & inrange(ft011e_3,0,1000000)

gen h`wv'fcamt = .
missing_financial ft011e_1 ft011e_2 ft011e_3 , result(h`wv'fcamt)
missing_h  h`wv'fcany, result(h`wv'fcamt)
replace h`wv'fcamt = chamt1 + chamt2 + chamt3 if !mi(chamt1) & !mi(chamt2) & !mi(chamt3)
label variable h`wv'fcamt "h`wv'fcamt:w`wv' amount transfer from children/grandchildren"

bysort h`wv'coupid: replace h`wv'fcamt = h`wv'fcamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fcamt = h`wv'fcamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop chamt1 chamt2 chamt3

***Any Transfers from Parents***
*wave 1 household any transfers from parents
gen h`wv'fpany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'fpany) wave(`wv')
replace h`wv'fpany = 0 if ft009_ == 5 | (ft009_ == 1 & (ft010_1 == 1 | inrange(ft010_1,8,33) | ///
		ft010_2 == 1 | inrange(ft010_2,8,33) | ft010_3 == 1 | inrange(ft010_3,8,33)))
replace h`wv'fpany = 1 if ft009_ == 1 & (inrange(ft010_1,2,7) | inrange(ft010_2,2,7) | inrange(ft010_3,2,7))
label variable h`wv'fpany "h`wv'fpany:w`wv' any transfers from parents"
label values h`wv'fpany trans

bysort h`wv'coupid: replace h`wv'fpany = h`wv'fpany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fpany = h`wv'fpany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

*list r`wv'livpar s`wv'livpar ft009_ ft010_1 ft010_2 ft010_3  h`wv'fpan if  h`wv'fpan==.m & _n<100
*pause
***Amount Transfer from Parents***
*wave 1 household amount transfers from parents
gen paamt1 = .
replace paamt1 = 0 if h`wv'fpany == 0 | (mi(ft010_1) & inw`wv' == 1) | ft010_1 == 1 | inrange(ft010_1,8,33) 
replace paamt1 = ft011e_1 if inrange(ft010_1,2,7) & inrange(ft011e_1,0,1000000)

gen paamt2 = .
replace paamt2 = 0 if h`wv'fpany == 0 | (mi(ft010_2) & inw`wv' == 1) | ft010_2 == 1 | inrange(ft010_2,8,33) 
replace paamt2 = ft011e_2 if inrange(ft010_2,2,7) & inrange(ft011e_2,0,1000000)

gen paamt3 = .
replace paamt3 = 0 if h`wv'fpany == 0 | (mi(ft010_3) & inw`wv' == 1) | ft010_3 == 1 | inrange(ft010_3,8,33) 
replace paamt3 = ft011e_3 if inrange(ft010_3,2,7) & inrange(ft011e_3,0,1000000)

gen h`wv'fpamt = .
missing_financial ft011e_1 ft011e_2 ft011e_3 , result(h`wv'fpamt)
missing_h h`wv'fpany, result(h`wv'fpamt)
replace h`wv'fpamt = paamt1 + paamt2 + paamt3 if !mi(paamt1) & !mi(paamt2) & !mi(paamt3)
label variable h`wv'fpamt "h`wv'fpamt:w`wv' amount transfer from parents"

bysort h`wv'coupid: replace h`wv'fpamt = h`wv'fpamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fpamt = h`wv'fpamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop paamt1 paamt2 paamt3

***Any Transfers from Other Relatives***
*wave 1 household any transfers from other relatives
gen h`wv'foany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'foany) wave(`wv')
replace h`wv'foany = 0 if ft009_ == 5 | (ft009_ == 1 & (inrange(ft010_1,2,7) | inrange(ft010_1,10,22) | inrange(ft010_1,29,33) | ///
	 inrange(ft010_2,2,7) | inrange(ft010_2,10,22) | inrange(ft010_2,29,33) | inrange(ft010_3,2,7) | inrange(ft010_3,10,22) | inrange(ft010_3,29,33)))
replace h`wv'foany = 1 if ft009_ == 1 & (inlist(ft010_1,1,8,9) | inrange(ft010_1,23,28) | inlist(ft010_2,1,8,9) | inrange(ft010_2,23,28) | ///
		inlist(ft010_3,1,8,9) | inrange(ft010_3,23,28))
label variable h`wv'foany "h`wv'foany:w`wv' any transfers from other relatives"
label values h`wv'foany trans

bysort h`wv'coupid: replace h`wv'foany = h`wv'foany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'foany = h`wv'foany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer from Other Relatives***
*wave 1 household amount transfers from other relatives
gen otamt1 = .
replace otamt1 = 0 if h`wv'foany == 0 | (mi(ft010_1) & inw`wv' == 1) | inrange(ft010_1,2,7) | inrange(ft010_1,10,22) | inrange(ft010_1,29,33)
replace otamt1 = ft011e_1 if (inlist(ft010_1,1,8,9) | inrange(ft010_1,23,28)) & inrange(ft011e_1,0,1000000)

gen otamt2 = .
replace otamt2 = 0 if h`wv'foany == 0 | (mi(ft010_2) & inw`wv' == 1) | inrange(ft010_2,2,7) | inrange(ft010_2,10,22) | inrange(ft010_2,29,33)
replace otamt2 = ft011e_2 if (inlist(ft010_2,1,8,9) | inrange(ft010_2,23,28)) & inrange(ft011e_2,0,1000000)

gen otamt3 = .
replace otamt3 = 0 if h`wv'foany == 0 | (mi(ft010_2) & inw`wv' == 1) | inrange(ft010_2,2,7) | inrange(ft010_2,10,22) | inrange(ft010_2,29,33)
replace otamt3 = ft011e_3 if (inlist(ft010_2,1,8,9) | inrange(ft010_3,23,28)) & inrange(ft011e_3,0,1000000)

gen h`wv'foamt = .
missing_financial ft011e_1 ft011e_2 ft011e_3, result(h`wv'foamt)
missing_h h`wv'foany, result(h`wv'foamt)
replace h`wv'foamt = otamt1 + otamt2 + otamt3 if !mi(otamt1) & !mi(otamt2) & !mi(otamt3)
label variable h`wv'foamt "h`wv'foamt:w`wv' amount transfer from other relatives"

bysort h`wv'coupid: replace h`wv'foamt = h`wv'foamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'foamt = h`wv'foamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop otamt1 otamt2 otamt3

***Any Transfers from Other Non-Relatives***
*wave 1 household any transfers from other non-relatives
gen h`wv'ffany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'ffany) wave(`wv')
replace h`wv'ffany = 0 if ft009_ == 5 | (ft009_ == 1 & (inrange(ft010_1,1,28) | inrange(ft010_1,1,28) | inrange(ft010_1,1,28)))		
replace h`wv'ffany = 1 if ft009_ == 1 & (inrange(ft010_1,29,33) | inrange(ft010_2,29,33) | inrange(ft010_3,29,33))
label variable h`wv'ffany "h`wv'ffany:w`wv' any transfers from other non-relatives"
label values h`wv'ffany trans

bysort h`wv'coupid: replace h`wv'ffany = h`wv'ffany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'ffany = h`wv'ffany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer from Other Non-Relatives***
*wave 1 household amount transfers from other non-relatives
gen ftamt1 = .
replace ftamt1 = 0 if h`wv'ffany == 0 | (mi(ft010_1) & inw`wv' == 1) | inrange(ft010_1,1,28) 
replace ftamt1 = ft011e_1 if inrange(ft010_1,29,33) & inrange(ft011e_1,0,1000000)

gen ftamt2 = .
replace ftamt2 = 0 if h`wv'ffany == 0 | (mi(ft010_2) & inw`wv' == 1) | inrange(ft010_2,1,28) 
replace ftamt2 = ft011e_2 if inrange(ft010_2,29,33) & inrange(ft011e_2,0,1000000)

gen ftamt3 = .
replace ftamt3 = 0 if h`wv'ffany == 0 | (mi(ft010_3) & inw`wv' == 1) | inrange(ft010_3,1,28) 
replace ftamt3 = ft011e_3 if inrange(ft010_3,29,33) & inrange(ft011e_3,0,1000000)

gen h`wv'ffamt = .
missing_financial ft011e_1 ft011e_2 ft011e_3 , result(h`wv'ffamt)
missing_h h`wv'ffany, result(h`wv'ffamt)
replace h`wv'ffamt = ftamt1 + ftamt2 + ftamt3 if !mi(ftamt1) & !mi(ftamt2) & !mi(ftamt3)
label variable h`wv'ffamt "h`wv'ffamt:w`wv' amount transfer from other non-relatives"

bysort h`wv'coupid: replace h`wv'ffamt = h`wv'ffamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'ffamt = h`wv'ffamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop ftamt1 ftamt2 ftamt3

***Total Amount Transfers Received***
*wave 1 household total amount transfers received
gen h`wv'frec = .
missing_h h`wv'fcamt h`wv'fpamt h`wv'foamt h`wv'ffamt, result(h`wv'frec)
replace h`wv'frec = h`wv'fcamt + h`wv'fpamt + h`wv'foamt + h`wv'ffamt if !mi(h`wv'fcamt) & !mi(h`wv'fpamt) & !mi(h`wv'foamt) & !mi(h`wv'ffamt)
label variable h`wv'frec "h`wv'frec:w`wv' total amount transfers received"

***Total Amount Transfers Given***
*wave 1 household total amount transfers given
gen h`wv'tgiv = .
missing_h h`wv'tcamt h`wv'tpamt h`wv'toamt h`wv'tfamt, result(h`wv'tgiv)
replace h`wv'tgiv = h`wv'tcamt + h`wv'tpamt + h`wv'toamt + h`wv'tfamt if !mi(h`wv'tcamt) & !mi(h`wv'tpamt) & !mi(h`wv'toamt) & !mi(h`wv'tfam)
label variable h`wv'tgiv "h`wv'tgiv:w`wv' total amount transfers given"

***Net Value Financial Transfers***
*wave 1 household net value financial transfers
gen h`wv'ftot = .
missing_h h`wv'fre h`wv'tgiv, result(h`wv'ftot)
replace h`wv'ftot = h`wv'frec - h`wv'tgiv if !mi(h`wv'frec) & !mi(h`wv'tgiv)
label variable h`wv'ftot "h`wv'ftot:w`wv' total net transfers"

***Any Weekly Social Activities***
*wave 1 respondent any weekly social activities
gen r`wv'socwk = .
missing_common ac002d1 ac002d2 ac002d3 ac002d4 ac002d5 ac002d6 ac002d7 ac002dno /// 
			ac003_1 ac003_2 ac003_3 ac003_4 ac003_5 ac003_6 ac003_7, result(r`wv'socwk) wave(`wv')
replace r`wv'socwk = 0 if ac002dno == 1 | (ac003_1 == 3 | ac003_2 == 3 | ac003_3 == 3 | ac003_4 == 3 | ac003_5 == 3 | ac003_6 == 3 | ac003_7 == 3)
replace r`wv'socwk = 1 if (ac002d1 == 1 & inlist(ac003_1,1,2)) | (ac002d2 == 1 & inlist(ac003_2,1,2)) | (ac002d3 == 1 & inlist(ac003_3,1,2)) | ///
		(ac002d4 == 1 & inlist(ac003_4,1,2)) |(ac002d5 == 1 & inlist(ac003_5,1,2)) |(ac002d6 == 1 & inlist(ac003_6,1,2)) |(ac002d7 == 1 & inlist(ac003_7,1,2))
label variable r`wv'socwk "r`wv'socwk:w`wv' r any weekly social activities"
label values r`wv'socwk wksoc

*wave 1 spouse any weekly social activites
gen s`wv'socwk = .
spouse r`wv'socwk, result(s`wv'socwk) wave(`wv')
label variable s`wv'socwk "s`wv'socwk:w`wv' s any weekly social activities"
label values s`wv'socwk wksoc

***Frequency Participate Religious Groups***
*wave 1 respondent frequency participate religious groups
gen r`wv'socrelg_s = .
missing_common ac002d6 ac002dno ac003_6, result(r`wv'socrelg_s) wave(`wv')
replace r`wv'socrelg_s = ac003_6 if inrange(ac003_6,1,3)
replace r`wv'socrelg_s = 4 if ac002d6 == 0 | ac002dno == 1
label variable r`wv'socrelg_s "r`wv'socrelg_s:w`wv' r freq participate religous groups"
label values r`wv'socrelg_s relfreq

*wave 1 spouse frequency participate religious groups
gen s`wv'socrelg_s = .
spouse r`wv'socrelg_s, result(s`wv'socrelg_s) wave(`wv')
label variable s`wv'socrelg_s "s`wv'socrelg_s:w`wv' s freq participate religous groups"
label values s`wv'socrelg_s relfreq

***Any Weekly Participation Religious Groups***
*wave 1 respondent any weekly religious groups
gen r`wv'relgwk = .
missing_common ac002d6 ac002dno ac003_6, result(r`wv'relgwk) wave(`wv')
replace r`wv'relgwk = 0 if ac002dno == 1 | ac002d6 == 0 | (ac002d6 == 1 & ac003_6 == 3)
replace r`wv'relgwk = 1 if ac002d6 == 1 & inlist(ac003_6,1,2)
label variable r`wv'relgwk "r`wv'relgwk:w`wv' r any weekly religious groups"
label values r`wv'relgwk wksoc

*wave 1 spouse any weekly religious groups
gen s`wv'relgwk = .
spouse r`wv'relgwk, result(s`wv'relgwk) wave(`wv')
label variable s`wv'relgwk "s`wv'relgwk:w`wv' s any weekly religious groups"
label values s`wv'relgwk wksoc


***drop wave 1 cover screen (respondent) file raw variables
drop `family_w1_cv_r'

***drop wave 1 Demographic file raw variables
drop `family_w1_dn'

***drop wave 1 Children file raw variables
drop `family_w1_ch'

***drop wave 1 imputation file raw variables***
drop `family_w1_imput'

***drop wave 1 sp file raw variables***
drop `family_w1_sp'

***drop wave 1 ft raw variables***
drop `family_w1_ft'

***drop wave 1 ac raw variables***
drop `family_w1_ac'

***drop wave 1 ph raw variables***
drop `family_w1_ph'


******************************************************************************************

*set wave number
local wv = 2

***merge with wave 2 cover screen (respondent) data***
local family_w2_cv_r hhsize country relrpers cvresp gender
merge 1:1 mergeid using "$wave_2_cv_r", keepusing(`family_w2_cv_r') nogen

***merge with wave 2 Demographic data***
local family_w2_dn dn034_ dn036_ dn037_ dn026_1 dn026_2 dn027_1 dn027_2 dn028_1 dn028_2 dn032_1 dn032_2 dn030_1 dn030_2
merge 1:1 mergeid using "$wave_2_dn", keepusing(`family_w2_dn') nogen

***merge with wave 2 Children data***
local family_w2_ch ch001_ ch021_ ch005_* ch007_* ch014_*
merge 1:1 mergeid using "$wave_2_ch", keepusing(`family_w2_ch') nogen

***merge with wave 2 imputation data***
local family_w2_imput nchild nchild_f ngrchild ngrchild_f fam_resp fin_resp hou_resp
merge 1:1 mergeid using "$wave_2_imputations_", keepusing(`family_w2_imput') nogen

***merge with wave 2 sp data***
local family_w2_sp sp002_ sp003_1 sp003_2 sp003_3 sp008_ sp009_1 sp009_2 sp009_3 sp014_ sp018_ sp020_ ///
		sp004d1_* sp004d2_* sp004d3_* sp019* sp021*
merge 1:1 mergeid using "$wave_2_sp", keepusing(`family_w2_sp') nogen

***merge with wave 2 ft data***
local family_w2_ft ft002_ ft003_* ft004e_* ft009_ ft010_* ft011e_*
merge 1:1 mergeid using "$wave_2_ft", keepusing(`family_w2_ft') nogen

***merge with wave 2 ac data***
local family_w2_ac ac002d* ac003_*
merge 1:1 mergeid using "$wave_2_ac", keepusing(`family_w2_ac') nogen

***merge with wave 2 physical health data***
local family_w2_ph ph048dno
merge 1:1 mergeid using "$wave_2_ph", keepusing(`family_w2_ph') nogen

***merge with wave 2 technical variables data***
local family_w2_techvar mn101_ 
merge 1:1 mergeid using "$wave_2_techvar", keepusing(`family_w2_techvar') nogen


***parental mortality: mother alive***
*wave 2 respondent mother alive status
tempvar mominhouse
egen `mominhouse' = total(relrpers == 5 & gender == 2), by(hhid2)
recode `mominhouse' (1/10 = 1)
gen r2momliv =.
missing_common dn026_1, result(r2momliv) wave(`wv')
replace r2momliv = 0 if dn026_1 == 5 | (r1momliv == 0 & inw2 == 1)
replace r2momliv = 1 if dn026_1 == 1 | (`mominhouse' == 1 & cvresp == 1)
label variable r2momliv "r2momliv:w2 r mother alive"

*wave 2 spouse mother alive status
gen s2momliv =.
spouse r2momliv, result(s2momliv) wave(2)
label variable s2momliv "s2momliv:w2 s mother alive"
label values r2momliv liv
label values s2momliv liv

***parental mortality: father alive***
*wave 2 respondent father alive status
tempvar dadinhouse
egen `dadinhouse' = total(relrpers == 5 & gender == 1), by(hhid2)
recode `dadinhouse' (1/10 = 1)
gen r2dadliv =.
missing_common dn026_2, result(r2dadliv) wave(`wv')
replace r2dadliv = 0 if dn026_2 == 5 | (r1dadliv == 0 & inw2 == 1)
replace r2dadliv = 1 if dn026_2 == 1 | (`dadinhouse' == 1 & cvresp == 1)
label variable r2dadliv "r2dadliv:w2 r father alive"

*wave 2 spouse father alive status
gen s2dadliv =.
spouse r2dadliv, result(s2dadliv) wave(2)
label variable s2dadliv "s2dadliv:w2 s father alive"
label values r2dadliv liv
label values s2dadliv liv

***parental mortality: mother's current age or age at death***
*wave 2 respondent mother's age
gen r2momage =.
missing_common dn026_1, result(r2momage) wave(`wv')
missing_common dn028_1 if dn026_1 == 1, result(r2momage) wave(`wv')
missing_common dn027_1 if dn026_1 == 5, result(r2momage) wave(`wv')
replace r2momage = r1momage if inrange(r1momage,1,120) & r1momliv == 0 & inw2 == 1
replace r2momage = dn028_1 if dn026_1 == 1 & inrange(dn028_1, 1, 120)
replace r2momage = r1momage + 2 if dn026_1 == 1 & dn028_1 == . & inrange(r1momage,1,120)
replace r2momage = dn027_1 if dn026_1 == 5 & inrange(dn027_1, 1, 120)
label variable r2momage "r2momage:w2 r mother age current/at death"

*wave 2 spouse mother's age
gen s2momage =.
spouse r2momage, result(s2momage) wave(2)
label variable s2momage "s2momage:w2 s mother age current/at death"

***parental mortality: father's current age or age at death***
*wave 2 respondent father's age
gen r2dadage =.
missing_common dn026_2, result(r2dadage) wave(`wv')
missing_common dn028_2 if dn026_2 == 1, result(r2dadage) wave(`wv')
missing_common dn027_2 if dn026_2 == 5, result(r2dadage) wave(`wv')
replace r2dadage = r1dadage if inrange(r1dadage,1,120) & r1dadliv == 0 & inw2 == 1
replace r2dadage = dn028_2 if dn026_2 == 1 & inrange(dn028_2, 1, 120)
replace r2dadage = r1dadage + 2 if dn026_2 == 1 & dn028_1 == . & inrange(r1dadage,1,120)
replace r2dadage = dn027_2 if dn026_2 == 5 & inrange(dn027_2, 1, 120)
label variable r2dadage "r2dadage:w2 r father age current/at death"

*wave 2 spouse father's age
gen s2dadage =.
spouse r2dadage, result(s2dadage) wave(2)
label variable s2dadage "s2dadage:w2 s father age current/at death"

***Number of people living in house***
*wave 2 household number of people living in house
gen hh2hhres = hhsize if inw2==1
label variable hh2hhres "hh2hhres:w2 Number of people in HH"

***Number of children***
*wave 2 household number of living children
gen h`wv'child = nchild
label variable h`wv'child "h`wv'child:w`wv' Number of living children R/P"

*wave 2 couple-level flag number of living children
chimputation_flag nchild_f, gen(h`wv'fchild) by(h`wv'coupid)
label variable h`wv'fchild "h`wv'fchild:w`wv' Impute Flag Num of living children"
label values h`wv'fchild fchild

***Number of grandchildren***
*wave 2 household number of grandchildren
gen h`wv'grchild = ngrchild
label variable h`wv'grchild "h`wv'grchild:w`wv' Number of grandchildren R/P"

*wave 2 couple-level flag number of grandchildren
chimputation_flag ngrchild_f, gen(h`wv'fgrchild) by(h`wv'coupid)
label variable h`wv'fgrchild "h`wv'fgrchild:w`wv' Impute Flag Num of grandchildren"
label values h`wv'fgrchild fchild

***Number of living siblings***
*wave 2 respondent number of living brothers
gen r2livbro =.
missing_common dn036_, result(r2livbro) wave(`wv')
replace r2livbro = r1livbro if !mi(r1livbro) & inw2==1
replace r2livbro = 0 if dn034_ == 5
replace r2livbro = dn036_ if inrange(dn036_,0,20)
label variable r2livbro "r2livbro:w2 r Number of living brothers"

*wave 2 respondent number of living sisters
gen r2livsis =.
missing_common dn037_, result(r2livsis) wave(`wv')
replace r2livsis = r1livsis if !mi(r1livsis) & inw2==1
replace r2livsis = 0 if dn034_ == 5
replace r2livsis = dn037_ if inrange(dn037_,0,20)
label variable r2livsis "r2livsis:w2 r Number of living sisters"

*wave 2 respondent number of living siblings
gen r2livsib =.
replace r2livsib = r2livbro + r2livsis
missing_h r2livbro r2livsis,result(r2livsib)
label variable r2livsib "r2livsib:w2 r Number of living siblings"

*wave 2 spouse number of living brothers
gen s2livbro =.
spouse r2livbro, result(s2livbro) wave(2)
label variable s2livbro "s2livbro:w2 s Number of living brothers"

*wave 2 spouse number of living sisters
gen s2livsis =.
spouse r2livsis, result(s2livsis) wave(2)
label variable s2livsis "s2livsis:w2 s Number of living sisters"

*wave 2 spouse number of living siblings
gen s2livsib =.
spouse r2livsib, result(s2livsib) wave(2)
label variable s2livsib "s2livsib:w2 s Number of living siblings"

***Number of living parents***
*wave 2 respondent number of living parents
gen r2livpar=.
missing_h r2momliv r2dadliv,result(r2livpar)
replace r2livpar=0 if !missing(r2momliv) & !missing(r2dadliv)
replace r2livpar = r2livpar + 1 if r2momliv==1 & !missing(r2dadliv)
replace r2livpar = r2livpar + 1 if r2dadliv==1 & !missing(r2momliv)
label variable r2livpar "r2livpar:w2 r Number of living parents"

*wave 2 spouse number of living parents
gen s2livpar =.
spouse r2livpar, result(s2livpar) wave(2)
label variable s2livpar "s2livpar:w2 s Number of living parents"

***Number of living sons/daughters***
*wave 2 household number of living sons
recode ch005_* (-2 -1 2 = 0), prefix(m)
egen h`wv'son = rowtotal(mch005_*) if inw`wv' == 1,m 
replace h`wv'son = .m if (ch005_1==. | ch005_2==. | ch005_3==. | ch005_4==. | /// 
	ch005_5==. | ch005_6==. | ch005_7==. | ch005_8==. | ch005_9==. | ch005_10==. | ///
	ch005_11==. | ch005_12==. | ch005_13==. | ch005_14==. | ch005_15==. | ch005_16==. | ///
	ch005_17==. | ch005_18==. | ch005_19==. | ch005_20==.) & inlist(h`wv'son,.,0) & inw`wv' == 1
replace h`wv'son = .d if (ch005_1==-1 | ch005_2==-1 | ch005_3==-1 | ch005_4==-1 | /// 
	ch005_5==-1 | ch005_6==-1 | ch005_7==-1 | ch005_8==-1 | ch005_9==-1 | ch005_11==-1 | ///
	ch005_11==-1 | ch005_12==-1 | ch005_13==-1 | ch005_14==-1 | ch005_15==-1 | ch005_16==-1 | ///
	ch005_17==-1 | ch005_18==-1 | ch005_19==-1 | ch005_20==-1) & inlist(h`wv'son,.,.m,0)
replace h`wv'son = .r if (ch005_1==-2 | ch005_2==-2 | ch005_3==-2 | ch005_4==-2 | /// 
	ch005_5==-2 | ch005_6==-2 | ch005_7==-2 | ch005_8==-2 | ch005_9==-2 | ch005_11==-2 | ///
	ch005_11==-2 | ch005_12==-2 | ch005_13==-2 | ch005_14==-2 | ch005_15==-2 | ch005_16==-2 | ///
	ch005_17==-2 | ch005_18==-2 | ch005_19==-2 | ch005_20==-2) & inlist(h`wv'son,.,.m,0)

*wave 2 household number of living daughters
recode ch005_* (-2 -1 1 = 0) (2 = 1), prefix(f)
egen h`wv'dau = rowtotal(fch005_*) if inw`wv' == 1,m
replace h`wv'dau = .m if (ch005_1==. | ch005_2==. | ch005_3==. | ch005_4==. | /// 
	ch005_5==. | ch005_6==. | ch005_7==. | ch005_8==. | ch005_9==. | ch005_11==. | ///
	ch005_11==. | ch005_12==. | ch005_13==. | ch005_14==. | ch005_15==. | ch005_16==. | ///
	ch005_17==. | ch005_18==. | ch005_19==. | ch005_20==.) & inlist(h`wv'dau,.,0) & inw`wv' == 1
replace h`wv'dau = .d if (ch005_1==-1 | ch005_2==-1 | ch005_3==-1 | ch005_4==-1 | /// 
	ch005_5==-1 | ch005_6==-1 | ch005_7==-1 | ch005_8==-1 | ch005_9==-1 | ch005_11==-1 | ///
	ch005_11==-1 | ch005_12==-1 | ch005_13==-1 | ch005_14==-1 | ch005_15==-1 | ch005_16==-1 | ///
	ch005_17==-1 | ch005_18==-1 | ch005_19==-1 | ch005_20==-1) & inlist(h`wv'dau,.,.m,0)
replace h`wv'dau = .r if (ch005_1==-2 | ch005_2==-2 | ch005_3==-2 | ch005_4==-2 | /// 
	ch005_5==-2 | ch005_6==-2 | ch005_7==-2 | ch005_8==-2 | ch005_9==-2 | ch005_11==-2 | ///
	ch005_11==-2 | ch005_12==-2 | ch005_13==-2 | ch005_14==-2 | ch005_15==-2 | ch005_16==-2 | ///
	ch005_17==-2 | ch005_18==-2 | ch005_19==-2 | ch005_20==-2) & inlist(h`wv'dau,.,.m,0)
replace h`wv'son = 0 if h`wv'child == 0 | ((h`wv'child == h`wv'dau) & !mi(h`wv'dau))
replace h`wv'dau = 0 if h`wv'child == 0 | ((h`wv'child == h`wv'son) & !mi(h`wv'son))
label variable h`wv'son "h`wv'son:w`wv' Number of living sons R/P"
label variable h`wv'dau "h`wv'dau:w`wv' Number of living daughters R/P"

bysort h`wv'coupid: replace h`wv'son = h`wv'son[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'son = h`wv'son[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'dau = h`wv'dau[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'dau = h`wv'dau[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

drop mch005_* fch005_*

***Any Child Co-Resides***
*wave 2 household any child co-resides
gen h`wv'coresd = .
missing_common ch007_1 ch007_2 ch007_3 ch007_4 ch007_5 ch007_6 ch007_7 ch007_8 ch007_9 ch007_10 ///
	 ch007_11 ch007_12 ch007_13 ch007_14 ch007_15 ch007_16 ch007_17 ch007_18 ch007_19 ch007_20, result(h`wv'coresd) wave(`wv')
replace h`wv'coresd = .k if nchild == 0
replace h`wv'coresd = 0 if nchild != 0 & (inrange(ch007_1,2,9) | inrange(ch007_2,2,9) | inrange(ch007_3,2,9) | inrange(ch007_4,2,9) | /// 
		inrange(ch007_5,2,9) | inrange(ch007_6,2,9) | inrange(ch007_7,2,9) | inrange(ch007_8,2,9) | inrange(ch007_9,2,9) | ///
		inrange(ch007_10,2,9) | inrange(ch007_11,2,9) | inrange(ch007_12,2,9) | inrange(ch007_13,2,9) | inrange(ch007_14,2,9) | ///
		inrange(ch007_15,2,9) | inrange(ch007_16,2,9) | inrange(ch007_17,2,9) | inrange(ch007_18,2,9) | inrange(ch007_19,2,9) | inrange(ch007_20,2,9))
replace h`wv'coresd = 1 if nchild != 0 & (ch007_1 == 1 | ch007_2 == 1 | ch007_3 == 1 | ch007_4 == 1 | ch007_5 == 1 | ch007_6 == 1 | ///
		ch007_7 == 1 | ch007_8 == 1 | ch007_9 == 1 | ch007_10 == 1 | ch007_11 == 1 | ch007_12 == 1 | ch007_13 == 1 | ///
		ch007_14 == 1 | ch007_15 == 1 | ch007_16 == 1 | ch007_17 == 1 | ch007_18 == 1 | ch007_19 == 1 | ch007_20 == 1)
label variable h`wv'coresd "h`wv'coresd:w`wv' any child coresides with R/P"
label values h`wv'coresd child

bysort h`wv'coupid: replace h`wv'coresd = h`wv'coresd[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'coresd = h`wv'coresd[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Child Lives Near***
*wave 2 household any child lives near
gen h`wv'lvnear = .
missing_common ch007_1 ch007_2 ch007_3 ch007_4 ch007_5 ch007_6 ch007_7 ch007_8 ch007_9 ch007_10 ///
	 ch007_11 ch007_12 ch007_13 ch007_14 ch007_15 ch007_16 ch007_17 ch007_18 ch007_19 ch007_20, result(h`wv'lvnear) wave(`wv')
replace h`wv'lvnear = .k if nchild == 0
replace h`wv'lvnear = 0 if nchild != 0 & (inrange(ch007_1,6,9) | inrange(ch007_2,6,9) | inrange(ch007_3,6,9) | inrange(ch007_4,6,9) | /// 
		inrange(ch007_5,6,9) | inrange(ch007_6,6,9) | inrange(ch007_7,6,9) | inrange(ch007_8,6,9) | inrange(ch007_9,6,9) | ///
		inrange(ch007_10,6,9) | inrange(ch007_11,6,9) | inrange(ch007_12,6,9) | inrange(ch007_13,6,9) | inrange(ch007_14,6,9) | ///
		inrange(ch007_15,6,9) | inrange(ch007_16,6,9) | inrange(ch007_17,6,9) | inrange(ch007_18,6,9) | inrange(ch007_19,6,9) | inrange(ch007_20,6,9))
replace h`wv'lvnear = 1 if nchild != 0 & (inrange(ch007_1,1,5) | inrange(ch007_2,1,5) | inrange(ch007_3,1,5) | inrange(ch007_4,1,5) | /// 
		inrange(ch007_5,1,5) | inrange(ch007_6,1,5) | inrange(ch007_7,1,5) | inrange(ch007_8,1,5) | inrange(ch007_9,1,5) | ///
		inrange(ch007_10,1,5) | inrange(ch007_11,1,5) | inrange(ch007_12,1,5) | inrange(ch007_13,1,5) | inrange(ch007_14,1,5) | ///
		inrange(ch007_15,1,5) | inrange(ch007_16,1,5) | inrange(ch007_17,1,5) | inrange(ch007_18,1,5) | inrange(ch007_19,1,5) | inrange(ch007_20,1,5))
label variable h`wv'lvnear "h`wv'lvnear:w`wv' any child lives within 30 min"
label values h`wv'lvnear child

bysort h`wv'coupid: replace h`wv'lvnear = h`wv'lvnear[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'lvnear = h`wv'lvnear[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Weekly Contact with Children***
*wave 2 household any weekly contact with children
gen h`wv'kcnt = .
missing_common ch014_1 ch014_2 ch014_3 ch014_4, result(h`wv'kcnt) wave(`wv')
replace h`wv'kcnt = .d if h`wv'coresd == .d
replace h`wv'kcnt = .r if h`wv'coresd == .r
replace h`wv'kcnt = .k if nchild == 0
replace h`wv'kcnt = 0 if nchild != 0 & (inrange(ch014_1,4,7) | inrange(ch014_2,4,7) | inrange(ch014_3,4,7) | ///
		inrange(ch014_4,4,7))
replace h`wv'kcnt = 1 if nchild != 0 & (inrange(ch014_1,1,3) | inrange(ch014_2,1,3) | inrange(ch014_3,1,3) | ///
		inrange(ch014_4,1,3) | h`wv'coresd == 1)
label variable h`wv'kcnt "h`wv'kcnt:w`wv' any weekly contact with children in person/phone/mail"
label values h`wv'kcnt contact

bysort h`wv'coupid: replace h`wv'kcnt = h`wv'kcnt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'kcnt = h`wv'kcnt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Frequency of Contact with Children***
*wave 2 household frequency contact with children
gen h`wv'kcntf = .
missing_common ch014_1 ch014_2 ch014_3 ch014_4, result(h`wv'kcntf) wave(`wv')
replace h`wv'kcntf = .d if h`wv'coresd == .d
replace h`wv'kcntf = .r if h`wv'coresd == .r
replace h`wv'kcntf = .k if nchild == 0
replace h`wv'kcntf = 7 if nchild != 0 & (ch014_1 == 7 | ch014_2 == 7 | ch014_3 == 7 | ch014_4 == 7)
replace h`wv'kcntf = 6 if nchild != 0 & (ch014_1 == 6 | ch014_2 == 6 | ch014_3 == 6 | ch014_4 == 6)
replace h`wv'kcntf = 5 if nchild != 0 & (ch014_1 == 5 | ch014_2 == 5 | ch014_3 == 5 | ch014_4 == 5)
replace h`wv'kcntf = 4 if nchild != 0 & (ch014_1 == 4 | ch014_2 == 4 | ch014_3 == 4 | ch014_4 == 4)
replace h`wv'kcntf = 3 if nchild != 0 & (ch014_1 == 3 | ch014_2 == 3 | ch014_3 == 3 | ch014_4 == 3)
replace h`wv'kcntf = 2 if nchild != 0 & (ch014_1 == 2 | ch014_2 == 2 | ch014_3 == 2 | ch014_4 == 2)
replace h`wv'kcntf = 1 if nchild != 0 & (h`wv'coresd == 1 | ch014_1 == 1 | ch014_2 == 1 | ch014_3 == 1 | ch014_4 == 1)
label variable h`wv'kcntf "h`wv'kcntf:w`wv' frequency contact with children in person/phone/mail"
label values h`wv'kcntf freqcont

bysort h`wv'coupid: replace h`wv'kcntf = h`wv'kcntf[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'kcntf = h`wv'kcntf[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Weekly Contact with Parents***
*wave 2 respondent any weekly contact with parents
gen r`wv'pcnt = .
missing_common dn032_1 dn032_2 dn030_1 dn030_2, result(r`wv'pcnt) wave(`wv')
replace r`wv'pcnt = .n if r`wv'livpar == 0
replace r`wv'pcnt = 0 if inrange(dn032_1,4,7) | inrange(dn032_2,4,7)
replace r`wv'pcnt = 1 if inrange(dn032_1,1,3) | inrange(dn032_2,1,3) | dn030_1 == 1 | dn030_2 == 1
label variable r`wv'pcnt "r`wv'pcnt:w`wv' r any weekly contact with parents in person/phone/mail"
label values r`wv'pcnt contact

*wave 2 spouse any weekly contact with parents
gen s`wv'pcnt = .
spouse r`wv'pcnt, result(s`wv'pcnt) wave(`wv')
label variable s`wv'pcnt "s`wv'pcnt:w`wv' s any weekly contact with parents in person/phone/mail"
label values s`wv'pcnt contact

***Frequency of Contact with Parents***
*wave 2 respondent frequency contact with parents
gen r`wv'pcntf = .
missing_common dn032_1 dn032_2 dn030_1 dn030_2, result(r`wv'pcntf) wave(`wv')
replace r`wv'pcntf = .n if r`wv'livpar == 0 
replace r`wv'pcntf = 7 if dn032_1 == 7 | dn032_2 == 7
replace r`wv'pcntf = 6 if dn032_1 == 6 | dn032_2 == 6
replace r`wv'pcntf = 5 if dn032_1 == 5 | dn032_2 == 5
replace r`wv'pcntf = 4 if dn032_1 == 4 | dn032_2 == 4
replace r`wv'pcntf = 3 if dn032_1 == 3 | dn032_2 == 3
replace r`wv'pcntf = 2 if dn032_1 == 2 | dn032_2 == 2
replace r`wv'pcntf = 1 if dn032_1 == 1 | dn032_2 == 1
label variable r`wv'pcntf "r`wv'pcntf:w`wv' r frequency contact with parents in person/phone/mail"
label values r`wv'pcntf freqcont

*wave 2 spouse frequency contact with parents
gen s`wv'pcntf = .
spouse r`wv'pcntf, result(s`wv'pcntf) wave(`wv')
label variable s`wv'pcntf "s`wv'pcntf:w`wv' s frequency contact with parents in person/phone/mail"
label values s`wv'pcntf freqcont

***Received Any Informal Care***
*wave 2 household received any informal care
gen hout = .
missing_common sp002_, result(hout) wave(`wv')
replace hout = 0 if sp002_ == 5
replace hout = 1 if sp002_ == 1
bysort h`wv'coupid: replace hout = hout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace hout = hout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rin = .
missing_common sp020_ hhsize ph048dno, result(rin) wave(`wv')
replace rin = 0 if sp020_ == 5 | hhsize == 1 | ph048dno == 1
replace rin = 1 if sp020_ == 1

gen spin = .
spouse rin, result(spin) wave(`wv')

gen h`wv'raany = .
replace h`wv'raany = .b if mi(spin) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .m if (hout == .m | rin == .m | spin == .m | r`wv'mstat == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .m if (hout == .m | rin == .m | r`wv'mstat == .m) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = .r if (hout == .r | rin == .r | spin == .r | r`wv'mstat == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .r if (hout == .r | rin == .r | r`wv'mstat == .r) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = .d if (hout == .d | rin == .d | spin == .d | r`wv'mstat == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .d if (hout == .d | rin == .d | r`wv'mstat == .d) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = 0 if (hout == 0 & rin == 0 & spin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = 0 if (hout == 0 & rin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = 1 if (hout == 1 | rin == 1 | spin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = 1 if (hout == 1 | rin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
label variable h`wv'raany "h`wv'raany:w`wv' received any informal care"
label values h`wv'raany carer

drop hout rin spin

***Received Informal Care from Children***
*wave 2 household received informal care from children
gen chout = .
missing_common sp002_ sp003_1 sp003_2 sp003_3 nchild, result(chout) wave(`wv')
replace chout = .k if nchild == 0
replace chout = 0 if nchild != 0 & (sp002_ == 5 | (sp002_ == 1 & ((inrange(sp003_1,1,9) | inrange(sp003_1,22,33)) | /// 
		(inrange(sp003_2,1,9) | inrange(sp003_2,22,33)) | (inrange(sp003_3,1,9) | inrange(sp003_3,22,33)))))
replace chout = 1 if nchild != 0 & (sp002_ == 1 & (inrange(sp003_1,10,21) | inrange(sp003_2,10,21) | inrange(sp003_3,10,21)))
bysort h`wv'coupid: replace chout = chout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace chout = chout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen crin = .
missing_common sp020_ sp021d10 sp021d11 sp021d12 sp021d13 sp021d14 sp021d15 sp021d16 sp021d17 sp021d18 sp021d19 ///
		sp021d20 sp021d21 hhsize ph048dno nchild, result(crin) wave(`wv')
replace crin = .k if nchild == 0
replace crin = 0 if hhsize == 1 | ph048dno == 1 | (nchild != 0 & (sp020_ == 5 | (sp020_ == 1 & (sp021d10 != 1 | sp021d11 != 1 | sp021d12 != 1 | ///
		sp021d13 != 1 | sp021d14 != 1 | sp021d15 != 1 | sp021d16 != 1 | sp021d17 != 1 | sp021d18 != 1 | sp021d19 != 1 | sp021d20 != 1 | sp021d21 != 1))))
replace crin = 1 if nchild != 0 & sp020_ == 1 & (sp021d10 == 1 | sp021d11 == 1 | sp021d12 == 1 | sp021d13 == 1 | ///
		sp021d14 == 1 | sp021d15 == 1 | sp021d16 == 1 | sp021d17 == 1 | sp021d18 == 1 | sp021d19 == 1 | sp021d20 == 1 | sp021d21 == 1)

gen cspin = .
spouse crin, result(cspin) wave (`wv')

gen h`wv'rccare = .
replace h`wv'rccare = .b if mi(cspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .m if (chout == .m | crin == .m | cspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .m if (chout == .m | crin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .r if (chout == .r | crin == .r | cspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .r if (chout == .r | crin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .d if (chout == .d | crin == .d | cspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .d if (chout == .d | crin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .k if (chout == .k | crin == .k | cspin == .k) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .k if (chout == .k | crin == .k) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = 0 if (chout == 0 & crin == 0 & cspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = 0 if (chout == 0 & crin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = 1 if (chout == 1 | crin == 1 | cspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = 1 if (chout == 1 | crin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
label variable h`wv'rccare "h`wv'rccare:w`wv' received informal care from children"
label values h`wv'rccare carer

drop chout crin cspin

***Received Informal Care from Relatives***
*wave 2 household received informal care from relatives
gen rhout = .
missing_common sp002_ sp003_1 sp003_2 sp003_3, result(rhout) wave(`wv')
replace rhout = 0 if sp002_ == 5 | (sp002_ == 1 & ((inrange(sp003_1,10,21) | inrange(sp003_1,29,33)) | /// 
		(inrange(sp003_2,10,21) | inrange(sp003_2,29,33)) | (inrange(sp003_3,11,21) | inrange(sp003_3,29,33))))
replace rhout = 1 if sp002_ == 1 & (inrange(sp003_1,1,9) | inrange(sp003_1,22,28) | inrange(sp003_2,1,9) | ///
		inrange(sp003_2,22,28) | inrange(sp003_3,1,9) | inrange(sp003_3,22,28))
bysort h`wv'coupid: replace rhout = rhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace rhout = rhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rrin = .
missing_common sp020_ sp021d1 sp021d2 sp021d3 sp021d4 sp021d5 sp021d6 sp021d7 sp021d8 sp021d9 sp021d22 sp021d23 sp021d24 sp021d25 ///
		sp021d26 sp021d27 sp021d28 hhsize ph048dno, result(rrin) wave(`wv')
replace rrin = 0 if hhsize == 1 | ph048dno == 1 | (sp020_ == 5 | (sp020_ == 1 & (sp021d1 != 1 | sp021d2 != 1 | sp021d3 != 1 | sp021d4 != 1 | ///
		sp021d5 != 1 | sp021d6 != 1 | sp021d7 != 1 | sp021d8 != 1 | sp021d9 != 1 | sp021d22 != 1 | sp021d23 != 1 | sp021d24 != 1 | ///
		sp021d25 != 1 | sp021d26 != 1 | sp021d27 != 1 | sp021d28 != 1)))
replace rrin = 1 if sp020_ == 1 & (sp021d1 == 1 | sp021d2 == 1 | sp021d3 == 1 | sp021d4 == 1 | ///
		sp021d5 == 1 | sp021d6 == 1 | sp021d7 == 1 | sp021d8 == 1 | sp021d9 == 1 | sp021d22 == 1 | sp021d23 == 1 | sp021d24 == 1 | ///
		sp021d25 == 1 | sp021d26 == 1 | sp021d27 == 1 | sp021d28 == 1)

gen rspin = .
spouse rrin, result(rspin) wave (`wv')

gen h`wv'rrcare = .
replace h`wv'rrcare = .b if mi(rspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .m if (rhout == .m | rrin == .m | rspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .m if (rhout == .m | rrin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = .r if (rhout == .r | rrin == .r | rspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .r if (rhout == .r | rrin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = .d if (rhout == .d | rrin == .d | rspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .d if (rhout == .d | rrin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = 0 if (rhout == 0 & rrin == 0 & rspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = 0 if (rhout == 0 & rrin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = 1 if (rhout == 1 | rrin == 1 | rspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = 1 if (rhout == 1 | rrin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
label variable h`wv'rrcare "h`wv'rrcare:w`wv' received informal care from relatives"
label values h`wv'rrcare carer

drop rhout rrin rspin

***Received Informal Care from non-relatives***
*wave 2 household received informal care from non-relatives
gen fhout = .
missing_common sp002_ sp003_1 sp003_2 sp003_3, result(fhout) wave(`wv')
replace fhout = 0 if sp002_ == 5 | (sp002_ == 1 & (inrange(sp003_1,1,28) | inrange(sp003_2,1,28) | inrange(sp003_3,1,28)))
replace fhout = 1 if sp002_ == 1 & (inrange(sp003_1,29,33) | inrange(sp003_2,29,33) | inrange(sp003_3,29,33))
bysort h`wv'coupid: replace fhout = fhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace fhout = fhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen frin = .
missing_common sp020_ sp021d29 sp021d30 sp021d31 sp021d32 sp021d33 hhsize ph048dno, result(frin) wave(`wv')
replace frin = 0 if hhsize == 1 | ph048dno == 1 | (sp020_ == 5 | (sp020_ == 1 & (sp021d29 != 1 | sp021d30 != 1 | sp021d31 != 1 | sp021d32 != 1 | ///
		sp021d33 != 1)))
replace frin = 1 if sp020_ == 1 & (sp021d29 == 1 | sp021d30 == 1 | sp021d31 == 1 | sp021d32 == 1 | ///
		sp021d33 == 1)

gen fspin = .
spouse frin, result(fspin) wave (`wv')

gen h`wv'rfcare = .
replace h`wv'rfcare = .b if mi(fspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .m if (fhout == .m | frin == .m | fspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .m if (fhout == .m | frin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = .r if (fhout == .r | frin == .r | fspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .r if (fhout == .r | frin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = .d if (fhout == .d | frin == .d | fspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .d if (fhout == .d | frin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = 0 if (fhout == 0 & frin == 0 & fspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = 0 if (fhout == 0 & frin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = 1 if (fhout == 1 | frin == 1 | fspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = 1 if (fhout == 1 | frin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
label variable h`wv'rfcare "h`wv'rfcare:w`wv' received informal care from non-relatives"
label values h`wv'rfcare carer

drop fhout frin fspin

***Gave Any Informal Care***
*wave 2 respondent gave any informal care 
gen rout = .
missing_common sp008_, result(rout) wave(`wv')
replace rout = 0 if sp008_ == 5
replace rout = 1 if sp008_ == 1

gen spout = .
spouse rout, result(spout) wave(`wv')

gen rin = .
missing_common sp018_ hhsize, result(rin) wave(`wv')
replace rin = 0 if sp018_ == 5 | hhsize == 1
replace rin = 1 if sp018_ == 1

gen spin = .
spouse rin, result(spin) wave(`wv')

gen h`wv'gcaany = .
replace h`wv'gcaany = .b if (mi(spout) | mi(spin)) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .m if (rout == .m | spout == .m | rin == .m | spin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .m if (rout == .m | rin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = .r if (rout == .r | spout == .r | rin == .r | spin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .r if (rout == .r | rin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = .d if (rout == .d | spout == .d | rin == .d | spin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .d if (rout == .d | rin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = 0 if (rout == 0 & spout == 0 & rin == 0 & spin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = 0 if (rout == 0 & rin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = 1 if (rout == 1 | spout == 1 | rin == 1 | spin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = 1 if (rout == 1 | rin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
label variable h`wv'gcaany "h`wv'gcaany:w`wv' gave any informal care"
label values h`wv'gcaany careg

drop rout spout rin spin

***Gave Informal Care to Children***
*wave 2 respondent gave informal care to children
gen crout = .
missing_common sp008_ sp009_1 sp009_2 sp009_3 nchild, result(crout) wave(`wv')
replace crout = .k if nchild == 0
replace crout = 0 if nchild != 0 & (sp008_ == 5 | (sp008_ == 1 & ((inrange(sp009_1,1,9) | /// 
		inrange(sp009_1,22,33)) | (inrange(sp009_2,1,9) | inrange(sp009_2,22,33)) | ///
		(inrange(sp009_3,1,9) | inrange(sp009_3,22,33)))))
replace crout = 1 if nchild != 0 & (sp008_ == 1 & (inrange(sp009_1,10,21) | inrange(sp009_2,10,21) | inrange(sp009_3,10,21)))

gen cspout = .
spouse crout, result(cspout) wave(`wv')

gen crin = .
missing_common sp018_ sp019d10 sp019d11 sp019d12 sp019d13 sp019d14 sp019d15 sp019d16 sp019d17 sp019d18 sp019d19 ///
		sp019d20 sp019d21 hhsize ph048dno nchild, result(crin) wave(`wv')
replace crin = .k if nchild == 0
replace crin = 0 if hhsize == 1 | ph048dno == 1 | (nchild != 0 & (sp018_ == 5 | (sp018_ == 1 & (sp019d10 != 1 | sp019d11 != 1 | sp019d12 != 1 | ///
		sp019d13 != 1 | sp019d14 != 1 | sp019d15 != 1 | sp019d16 != 1 | sp019d17 != 1 | sp019d18 != 1 | sp019d19 != 1 | sp019d20 != 1 | sp019d21 != 1))))
replace crin = 1 if nchild != 0 & sp018_ == 1 & (sp019d10 == 1 | sp019d11 == 1 | sp019d12 == 1 | sp019d13 == 1 | ///
		sp019d14 == 1 | sp019d15 == 1 | sp019d16 == 1 | sp019d17 == 1 | sp019d18 == 1 | sp019d19 == 1 | sp019d20 == 1 | sp019d21 == 1)

gen cspin = .
spouse crin, result(cspin) wave(`wv')

gen h`wv'gccare = .
replace h`wv'gccare = .b if (mi(cspout) | mi(cspin)) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .m if (crout == .m | cspout == .m | crin == .m | cspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .m if (crout == .m | crin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .r if (crout == .r | cspout == .r | crin == .r | cspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .r if (crout == .r | crin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .d if (crout == .d | cspout == .d | crin == .d | cspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .d if (crout == .d | crin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .k if (crout == .k | cspout == .k | crin == .k | cspin == .k) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .k if (crout == .k | crin == .k) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = 0 if (crout == 0 & cspout == 0 & crin == 0 & cspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = 0 if (crout == 0 & crin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = 1 if (crout == 1 | cspout == 1 | crin == 1 | cspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = 1 if (crout == 1 | crin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
label variable h`wv'gccare "h`wv'gccare:w`wv' gave informal care to children"
label values h`wv'gccare careg

drop crout cspout crin cspin

***Gave Informal Care to Relatives***
*wave 2 respondent gave informal care to relatives
gen rrout = .
missing_common sp008_ sp009_1 sp009_2 sp009_3, result(rrout) wave(`wv')
replace rrout = 0 if sp008_ == 5 | (sp008_ == 1 & (inrange(sp009_1,10,21) | inrange(sp009_1,29,33) | inrange(sp009_2,10,21) | ///
		inrange(sp009_2,29,33) | inrange(sp009_3,10,21) | inrange(sp009_3,29,33)))
replace rrout = 1 if sp008_ == 1 & (inrange(sp009_1,1,9) | inrange(sp009_1,22,28) | inrange(sp009_2,1,9) | inrange(sp009_2,22,28) | ///
		inrange(sp009_3,1,9) | inrange(sp009_3,22,28))

gen rspout = .
spouse rrout, result(rspout) wave(`wv')

gen rrin = .
missing_common sp018_ sp019d1 sp019d2 sp019d3 sp019d4 sp019d5 sp019d6 sp019d7 sp019d8 sp019d9 ///
		sp019d23 sp019d24 sp019d25 sp019d26 sp019d27 sp019d28 hhsize ph048dno, result(rrin) wave(`wv')
replace rrin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & (sp019d1 != 1 | sp019d2 != 1 | sp019d3 != 1 | ///
		sp019d4 != 1 | sp019d5 != 1 | sp019d6 != 1 | sp019d7 != 1 | sp019d8 != 1 | sp019d9 != 1 | sp019d23 != 1 | sp019d24 != 1 | ///
		sp019d25 != 1 | sp019d26 != 1 | sp019d27 != 1 | sp019d28 != 1)))
replace rrin = 1 if sp018_ == 1 & (sp019d1 == 1 | sp019d2 == 1 | sp019d3 == 1 | sp019d4 == 1 | sp019d5 == 1 | sp019d6 == 1 | sp019d7 == 1 | ///
		sp019d8 == 1 | sp019d9 == 1 | sp019d23 == 1 | sp019d24 == 1 | sp019d25 == 1 | sp019d26 == 1 | sp019d27 == 1 | sp019d28 == 1)

gen rspin = .
spouse rrin, result(rspin) wave(`wv')

gen h`wv'grcare = .
replace h`wv'grcare = .b if (mi(rspout) | mi(rspin)) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .m if (rrout == .m | rspout == .m | rrin == .m | rspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .m if (rrout == .m | rrin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = .r if (rrout == .r | rspout == .r | rrin == .r | rspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .r if (rrout == .r | rrin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = .d if (rrout == .d | rspout == .d | rrin == .d | rspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .d if (rrout == .d | rrin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = 0 if (rrout == 0 & rspout == 0 & rrin == 0 & rspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = 0 if (rrout == 0 & rrin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = 1 if (rrout == 1 | rspout == 1 | rrin == 1 | rspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = 1 if (rrout == 1 | rrin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
label variable h`wv'grcare "h`wv'grcare:w`wv' gave informal care to relatives"
label values h`wv'grcare careg

drop rrout rspout rrin rspin

***Gave Informal Care to non-relatives***
*wave 2 respondent gave informal care to non-relatives
gen frout = .
missing_common sp008_ sp009_1 sp009_2 sp009_3, result(frout) wave(`wv')
replace frout = 0 if sp008_ == 5 | (sp008_ == 1 & (inrange(sp009_1,1,28) | inrange(sp009_2,1,28) | inrange(sp009_3,1,28)))
replace frout = 1 if sp008_ == 1 & (inrange(sp009_1,29,33) | inrange(sp009_2,29,33) | inrange(sp009_3,29,33))

gen fspout = .
spouse frout, result(fspout) wave(`wv')

gen frin = .
missing_common sp018_ sp019d29 sp019d30 sp019d31 sp019d32 sp019d33 hhsize ph048dno, result(frin) wave(`wv')
replace frin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & (sp019d29 != 1 | sp019d30 != 1 | sp019d31 != 1 | ///
		sp019d32 != 1 | sp019d33 != 1)))
replace frin = 1 if sp018_ == 1 & (sp019d29 == 1 | sp019d30 == 1 | sp019d31 == 1 | sp019d32 == 1 | sp019d33 == 1)

gen fspin = .
spouse frin, result(fspin) wave(`wv')

gen h`wv'gfcare = .
replace h`wv'gfcare = .b if (mi(fspout) | mi(fspin)) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .m if (frout == .m | fspout == .m | frin == .m | fspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .m if (frout == .m | frin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = .r if (frout == .r | fspout == .r | frin == .r | fspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .r if (frout == .r | frin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = .d if (frout == .d | fspout == .d | frin == .d | fspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .d if (frout == .d | frin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = 0 if (frout == 0 & fspout == 0 & frin == 0 & fspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = 0 if (frout == 0 & frin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = 1 if (frout == 1 | fspout == 1 | frin == 1 | fspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = 1 if (frout == 1 | frin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
label variable h`wv'gfcare "h`wv'gfcare:w`wv' gave informal care to non-relatives"
label values h`wv'gfcare careg

drop frout fspout frin fspin

***Gave Informal Care to Grandchildren***
*wave 2 respondent gave care to grandchildren
gen krout = .
missing_common sp014_ ngrchild, result(krout) wave(`wv')
replace krout = .g if ngrchild == 0
replace krout = 0 if ngrchild != 0 & sp014_ == 5
replace krout = 1 if ngrchild != 0 & sp014_ == 1

gen kspout = .
spouse krout, result(kspout) wave(`wv')

gen krin = .
missing_common sp018_ sp019d22 hhsize ph048dno, result(krin) wave(`wv')
replace krin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & sp019d22 != 1))
replace krin = 1 if sp018_ == 1 & sp019d22 == 1

gen kspin = .
spouse krin, result(kspin) wave(`wv')

gen h`wv'gkcare = .
replace h`wv'gkcare = .b if (mi(kspout) | mi(kspin)) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .m if (krout == .m | kspout == .m | krin == .m | kspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .m if (krout == .m | krin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .r if (krout == .r | kspout == .r | krin == .r | kspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .r if (krout == .r | krin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .d if (krout == .d | kspout == .d | krin == .d | kspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .d if (krout == .d | krin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .g if (krout == .g | kspout == .g | krin == .g | kspin == .g) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .g if (krout == .g | krin == .g) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = 0 if (krout == 0 & kspout == 0 & krin == 0 & kspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = 0 if (krout == 0 & krin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = 1 if (krout == 1 | kspout == 1 | krin == 1 | kspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = 1 if (krout == 1 | krin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
label variable h`wv'gkcare "h`wv'gkcare:w`wv' gave care to grandchildren"
label values h`wv'gkcare careg

drop krout kspout krin kspin

***Receives Help with ADLs***
*wave 2 household receives help with ADLs
gen hadl = .
missing_common sp002_ sp004d1_1 sp004d1_2 sp004d1_3 sp004d2_1 sp004d2_2 sp004d2_3 ///
		sp004d3_1 sp004d3_2 sp004d3_3, result(hadl) wave(`wv')
replace hadl = 0 if sp002_ == 5 | (sp002_ == 1 & (inlist(sp004d1_1,2,3) | inlist(sp004d1_2,2,3) | inlist(sp004d1_3,2,3) | ///
		 inlist(sp004d2_1,2,3) | inlist(sp004d2_2,2,3) | inlist(sp004d2_3,2,3) | inlist(sp004d3_1,2,3) | inlist(sp004d3_2,2,3) | inlist(sp004d3_3,2,3))) 
replace hadl = 1 if sp002 == 1 & (sp004d1_1 == 1 | sp004d1_2 == 1 | sp004d1_3 == 1 | sp004d2_1 == 1 | sp004d2_2 == 1 | ///
			sp004d2_3 == 1 | sp004d3_1 == 1 | sp004d3_2 == 1 | sp004d3_3 == 1) 
bysort h`wv'coupid: replace hadl = hadl[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace hadl = hadl[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen radl = .
missing_common sp020_ hhsize ph048dno, result(radl) wave(`wv')
replace radl = 0 if sp020_ == 5 | hhsize == 1 | ph048dno == 1
replace radl = 1 if sp020_ == 1

gen spadl = .
spouse radl, result(spadl) wave(`wv')

gen h`wv'adlcare = .
replace h`wv'adlcare = .b if mi(spadl) & inlist(r`wv'mstat,1,3)
replace h`wv'adlcare = .m if (hadl == .m | radl == .m | spadl == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'adlcare = .m if (hadl == .m | radl == .m) & (!inlist(r`wv'mstat,1,3) | inlist(spadl,.u,.v))
replace h`wv'adlcare = .d if (hadl == .d | radl == .d | spadl == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'adlcare = .d if (hadl == .d | radl == .d) & (!inlist(r`wv'mstat,1,3) | inlist(spadl,.u,.v))
replace h`wv'adlcare = .r if (hadl == .r | radl == .r | spadl == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'adlcare = .r if (hadl == .r | radl == .r) & (!inlist(r`wv'mstat,1,3) | inlist(spadl,.u,.v))
replace h`wv'adlcare = 0 if (hadl == 0 & radl == 0 & spadl == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'adlcare = 0 if (hadl == 0 & radl == 0) & (!inlist(r`wv'mstat,1,3) | inlist(spadl,.u,.v))
replace h`wv'adlcare = 1 if (hadl == 1 | radl == 1 | spadl == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'adlcare = 1 if (hadl == 1 | radl == 1) & (!inlist(r`wv'mstat,1,3) | inlist(spadl,.u,.v))
label variable h`wv'adlcare "h`wv'adlcare:w`wv' received help with ADLs"
label values h`wv'adlcare carer

drop hadl radl spadl

***Any Transfer to Children/Grandchildren***
*wave 2 household any transfer to children/grandchildren
gen h`wv'tcany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'tcany) wave(`wv')
replace h`wv'tcany = 0 if ft002_ == 5 | (ft002_ == 1 & ((inrange(ft003_1,1,9) | inrange(ft003_1,23,33) | inrange(ft003_2,1,9) | /// 
		inrange(ft003_2,23,33) | inrange(ft003_3,1,9) | inrange(ft003_3,23,33))))
replace h`wv'tcany = 1 if ft002_ == 1 & (inrange(ft003_1,10,22) | inrange(ft003_2,10,22) | inrange(ft003_3,10,22))
label variable h`wv'tcany "h`wv'tcany:w`wv' any transfer to children/grandchildren"
label values h`wv'tcany trans

bysort h`wv'coupid: replace h`wv'tcany = h`wv'tcany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tcany = h`wv'tcany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer to Children/Grandchildren***
*wave 2 household amount transfer to children/grandchildren
gen kamt1 = .
replace kamt1 = 0 if h`wv'tcany == 0 | (mi(ft003_1) & inw`wv' == 1) | inrange(ft003_1,1,9) | inrange(ft003_1,23,33)
replace kamt1 = ft004e_1 if inrange(ft003_1,10,22) & inrange(ft004e_1,0,1000000)

gen kamt2 = .
replace kamt2 = 0 if h`wv'tcany == 0 | (mi(ft003_2) & inw`wv' == 1) | inrange(ft003_2,1,9) | inrange(ft003_2,23,33)
replace kamt2 = ft004e_2 if inrange(ft003_2,10,22) & inrange(ft004e_2,0,1000000)

gen kamt3 = .
replace kamt3 = 0 if h`wv'tcany == 0 | (mi(ft003_3) & inw`wv' == 1) | inrange(ft003_3,1,9) | inrange(ft003_3,23,33)
replace kamt3 = ft004e_3 if inrange(ft003_3,10,22) & inrange(ft004e_3,0,1000000)

gen h`wv'tcamt = .
missing_financial ft004e_1 ft004e_1 ft004e_3, result(h`wv'tcamt)
missing_h h`wv'tcany, result(h`wv'tcamt)
replace h`wv'tcamt = kamt1 + kamt2 + kamt3 if !mi(kamt1) & !mi(kamt2) & !mi(kamt3)
label variable h`wv'tcamt "h`wv'tcamt:w`wv' amount transfer to children/grandchildren"

bysort h`wv'coupid: replace h`wv'tcamt = h`wv'tcamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tcamt = h`wv'tcamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop kamt1 kamt2 kamt3

***Any Transfers to Parents***
*wave 2 household any transfers to parents
gen h`wv'tpany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'tpany) wave(`wv')
replace h`wv'tpany = 0 if ft002_ == 5 | (ft002_ == 1 & (ft003_1 == 1 | inrange(ft003_1,8,33) | ///
		ft003_2 == 1 | inrange(ft003_2,8,33) | ft003_3 == 1 | inrange(ft003_3,8,33)))
replace h`wv'tpany = 1 if ft002_ == 1 & (inrange(ft003_1,2,7) | inrange(ft003_2,2,7) | inrange(ft003_3,2,7))
label variable h`wv'tpany "h`wv'tpany:w`wv' any transfer to parents"
label values h`wv'tpany trans

bysort h`wv'coupid: replace h`wv'tpany = h`wv'tpany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tpany = h`wv'tpany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer to Parents***
*wave 2 household amount transfer to parents
gen pamt1 = .
replace pamt1 = 0 if h`wv'tpany == 0 | (mi(ft003_1) & inw`wv' == 1) | ft003_1 == 1 | inrange(ft003_1,8,33) 
replace pamt1 = ft004e_1 if inrange(ft003_1,2,7) & inrange(ft004e_1,0,1000000)

gen pamt2 = .
replace pamt2 = 0 if h`wv'tpany == 0 | (mi(ft003_2) & inw`wv' == 1) | ft003_2 == 1 | inrange(ft003_2,8,33) 
replace pamt2 = ft004e_2 if inrange(ft003_2,2,7) & inrange(ft004e_2,0,1000000)

gen pamt3 = .
replace pamt3 = 0 if h`wv'tpany == 0 | (mi(ft003_3) & inw`wv' == 1) | ft003_3 == 1 | inrange(ft003_3,8,33) 
replace pamt3 = ft004e_3 if inrange(ft003_3,2,7) & inrange(ft004e_3,0,1000000)

gen h`wv'tpamt = .
missing_financial ft004e_1 ft004e_2 ft004e_3 , result(h`wv'tpamt)
missing_h h`wv'tpany, result(h`wv'tpamt)
replace h`wv'tpamt = pamt1 + pamt2 + pamt3 if !mi(pamt1) & !mi(pamt2) & !mi(pamt3)
label variable h`wv'tpamt "h`wv'tpamt:w`wv' amount transfer to parents"

bysort h`wv'coupid: replace h`wv'tpamt = h`wv'tpamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tpamt = h`wv'tpamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop pamt1 pamt2 pamt3 

***Any Transfers to Other Relatives***
*wave 2 household any transfers to other relatives
gen h`wv'toany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'toany) wave(`wv')
replace h`wv'toany = 0 if ft002_ == 5 | (ft002_ == 1 & (inrange(ft003_1,2,7) | inrange(ft003_1,10,22) | inrange(ft003_1,29,33) | /// 
		inrange(ft003_2,2,7) | inrange(ft003_2,10,22) | inrange(ft003_2,29,33) | inrange(ft003_3,2,7) | inrange(ft003_3,10,22) | inrange(ft003_3,29,33)))
replace h`wv'toany = 1 if ft002_ == 1 & (inlist(ft003_1,1,8,9) | inrange(ft003_1,23,28) | inlist(ft003_2,1,8,9) | ///
		inrange(ft003_2,23,28) | inlist(ft003_3,1,8,9) | inrange(ft003_3,23,28))
label variable h`wv'toany "h`wv'toany:w`wv' any transfer to other relatives"
label values h`wv'toany trans

bysort h`wv'coupid: replace h`wv'toany = h`wv'toany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'toany = h`wv'toany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer to Other Relatives***
*wave 2 household amount transfer to other relatives
gen oamt1 = .
replace oamt1 = 0 if h`wv'toany == 0 | (mi(ft003_1) & inw`wv' == 1) | inrange(ft003_1,2,7) | inrange(ft003_1,10,22) | inrange(ft003_1,29,33)
replace oamt1 = ft004e_1 if (inlist(ft003_1,1,8,9) | inrange(ft003_1,23,28)) & inrange(ft004e_1,0,1000000)

gen oamt2 = .
replace oamt2 = 0 if h`wv'toany == 0 | (mi(ft003_2) & inw`wv' == 1) | inrange(ft003_2,2,7) | inrange(ft003_2,10,22) | inrange(ft003_2,29,33)
replace oamt2 = ft004e_2 if (inlist(ft003_2,1,8,9) | inrange(ft003_2,23,28)) & inrange(ft004e_2,0,1000000)

gen oamt3 = .
replace oamt3 = 0 if h`wv'toany == 0 | (mi(ft003_3) & inw`wv' == 1) | inrange(ft003_3,2,7) | inrange(ft003_3,10,22) | inrange(ft003_3,29,33)
replace oamt3 = ft004e_3 if (inlist(ft003_3,1,8,9) | inrange(ft003_3,23,28)) & inrange(ft004e_3,0,1000000)

gen h`wv'toamt = .
missing_financial ft004e_1 ft004e_2 ft004e_3, result(h`wv'toamt)
missing_h h`wv'toany, result(h`wv'toamt)
replace h`wv'toamt = oamt1 + oamt2 + oamt3 if !mi(oamt1) & !mi(oamt2) & !mi(oamt3)
label variable h`wv'toamt "h`wv'toamt:w`wv' amount transfer to other relatives"

bysort h`wv'coupid: replace h`wv'toamt = h`wv'toamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'toamt = h`wv'toamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop oamt1 oamt2 oamt3

***Any Transfers to Other Non-Relatives***
*wave 2 household any transfers to other non-relatives
gen h`wv'tfany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'tfany) wave(`wv')
replace h`wv'tfany = 0 if ft002_ == 5 | (ft002_ == 1 & (inrange(ft003_1,1,28) | inrange(ft003_2,1,28) | inrange(ft003_3,1,28)))
replace h`wv'tfany = 1 if ft002_ == 1 & (inrange(ft003_1,29,33) | inrange(ft003_2,29,33) | inrange(ft003_3,29,33))
label variable h`wv'tfany "h`wv'tfany:w`wv' any transfer to other non-relatives"
label values h`wv'tfany trans

bysort h`wv'coupid: replace h`wv'tfany = h`wv'tfany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tfany = h`wv'tfany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer to Other Non-Relatives***
*wave 2 household amount transfer to other non-relatives
gen famt1 = .
replace famt1 = 0 if h`wv'tfany == 0 | (mi(ft003_1) & inw`wv' == 1) | inrange(ft003_1,1,28) 
replace famt1 = ft004e_1 if inrange(ft003_1,29,33) & inrange(ft004e_1,0,1000000)

gen famt2 = .
replace famt2 = 0 if h`wv'tfany == 0 | (mi(ft003_2) & inw`wv' == 1) | inrange(ft003_2,1,28) 
replace famt2 = ft004e_2 if inrange(ft003_2,29,33) & inrange(ft004e_2,0,1000000)

gen famt3 = .
replace famt3 = 0 if h`wv'tfany == 0 | (mi(ft003_3) & inw`wv' == 1) | inrange(ft003_3,1,28) 
replace famt3 = ft004e_3 if inrange(ft003_3,29,33) & inrange(ft004e_3,0,1000000)

gen h`wv'tfamt = .
missing_financial ft004e_1 ft004e_2 ft004e_3 , result(h`wv'tfamt)
missing_h h`wv'tfany, result(h`wv'tfamt)
replace h`wv'tfamt = famt1 + famt2 + famt3 if !mi(famt1) & !mi(famt2) & !mi(famt3)
label variable h`wv'tfamt "h`wv'tfamt:w`wv' amount transfer to other non-relatives"

bysort h`wv'coupid: replace h`wv'tfamt = h`wv'tfamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tfamt = h`wv'tfamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop famt1 famt2 famt3

***Any Transfers from Children/Grandchildren***
*wave 2 household any transfers from children/grandchildren
gen h`wv'fcany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'fcany) wave(`wv')
replace h`wv'fcany = 0 if ft009_ == 5 | (ft009_ == 1 & (inrange(ft010_1,1,9) | inrange(ft010_1,23,33) | inrange(ft010_2,1,9) | ///
		 inrange(ft010_2,23,33) | inrange(ft010_3,1,9) | inrange(ft010_3,23,33)))
replace h`wv'fcany = 1 if ft009_ == 1 & (inrange(ft010_1,10,22) | inrange(ft010_2,10,22) | inrange(ft010_3,10,22))
label variable h`wv'fcany "h`wv'fcany:w`wv' any transfers from children/grandchildren"
label values h`wv'fcany trans

bysort h`wv'coupid: replace h`wv'fcany = h`wv'fcany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fcany = h`wv'fcany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer from Children/Grandchildren***
*wave 2 household amount transfers from children/grandchildren
gen chamt1 = .
replace chamt1 = 0 if h`wv'fcany == 0 | (mi(ft010_1) & inw`wv' == 1) | inrange(ft010_1,1,9) | inrange(ft010_1,23,33)
replace chamt1 = ft011e_1 if inrange(ft010_1,10,22) & inrange(ft011e_1,0,1000000)

gen chamt2 = .
replace chamt2 = 0 if h`wv'fcany == 0 | (mi(ft010_2) & inw`wv' == 1) | inrange(ft010_2,1,9) | inrange(ft010_2,23,33)
replace chamt2 = ft011e_2 if inrange(ft010_2,10,22) & inrange(ft011e_2,0,1000000)

gen chamt3 = .
replace chamt3 = 0 if h`wv'fcany == 0 | (mi(ft010_3) & inw`wv' == 1) | inrange(ft010_3,1,9) | inrange(ft010_3,23,33)
replace chamt3 = ft011e_3 if inrange(ft010_3,10,22) & inrange(ft011e_3,0,1000000)

gen h`wv'fcamt = .
missing_financial ft011e_1 ft011e_2 ft011e_3 , result(h`wv'fcamt)
missing_h  h`wv'fcany, result(h`wv'fcamt)
replace h`wv'fcamt = chamt1 + chamt2 + chamt3 if !mi(chamt1) & !mi(chamt2) & !mi(chamt3)
label variable h`wv'fcamt "h`wv'fcamt:w`wv' amount transfer from children/grandchildren"

bysort h`wv'coupid: replace h`wv'fcamt = h`wv'fcamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fcamt = h`wv'fcamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop chamt1 chamt2 chamt3

***Any Transfers from Parents***
*wave 2 household any transfers from parents
gen h`wv'fpany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'fpany) wave(`wv')
replace h`wv'fpany = 0 if ft009_ == 5 | (ft009_ == 1 & (ft010_1 == 1 | inrange(ft010_1,8,33) | ft010_2 == 1 | inrange(ft010_2,8,33) | ///
		 ft010_3 == 1 | inrange(ft010_3,8,33)))
replace h`wv'fpany = 1 if ft009_ == 1 & (inrange(ft010_1,2,7) | inrange(ft010_2,2,7) | inrange(ft010_3,2,7))
label variable h`wv'fpany "h`wv'fpany:w`wv' any transfers from parents"
label values h`wv'fpany trans

bysort h`wv'coupid: replace h`wv'fpany = h`wv'fpany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fpany = h`wv'fpany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer from Parents***
*wave 2 household amount transfers from parents
gen paamt1 = .
replace paamt1 = 0 if h`wv'fpany == 0 | (mi(ft010_1) & inw`wv' == 1) | ft010_1 == 1 | inrange(ft010_1,8,33) 
replace paamt1 = ft011e_1 if inrange(ft010_1,2,7) & inrange(ft011e_1,0,1000000)

gen paamt2 = .
replace paamt2 = 0 if h`wv'fpany == 0 | (mi(ft010_2) & inw`wv' == 1) | ft010_2 == 1 | inrange(ft010_2,8,33) 
replace paamt2 = ft011e_2 if inrange(ft010_2,2,7) & inrange(ft011e_2,0,1000000)

gen paamt3 = .
replace paamt3 = 0 if h`wv'fpany == 0 | (mi(ft010_3) & inw`wv' == 1) | ft010_3 == 1 | inrange(ft010_3,8,33) 
replace paamt3 = ft011e_3 if inrange(ft010_3,2,7) & inrange(ft011e_3,0,1000000)

gen h`wv'fpamt = .
missing_financial ft011e_1 ft011e_2 ft011e_3 , result(h`wv'fpamt)
missing_h h`wv'fpany, result(h`wv'fpamt)
replace h`wv'fpamt = paamt1 + paamt2 + paamt3 if !mi(paamt1) & !mi(paamt2) & !mi(paamt3)
label variable h`wv'fpamt "h`wv'fpamt:w`wv' amount transfer from parents"

bysort h`wv'coupid: replace h`wv'fpamt = h`wv'fpamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fpamt = h`wv'fpamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop paamt1 paamt2 paamt3

***Any Transfers from Other Relatives***
*wave 2 household any transfers from other relatives
gen h`wv'foany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'foany) wave(`wv')
replace h`wv'foany = 0 if ft009_ == 5 | (ft009_ == 1 & (inrange(ft010_1,2,7) | inrange(ft010_1,10,22) | inrange(ft010_1,29,33) | ///
	 inrange(ft010_2,2,7) | inrange(ft010_2,10,22) | inrange(ft010_2,29,33) | inrange(ft010_3,2,7) | inrange(ft010_3,10,22) | inrange(ft010_3,29,33)))
replace h`wv'foany = 1 if ft009_ == 1 & (inlist(ft010_1,1,8,9) | inrange(ft010_1,23,28) | inlist(ft010_2,1,8,9) | inrange(ft010_2,23,28) | ///
		inlist(ft010_3,1,8,9) | inrange(ft010_3,23,28))
label variable h`wv'foany "h`wv'foany:w`wv' any transfers from other relatives"
label values h`wv'foany trans

bysort h`wv'coupid: replace h`wv'foany = h`wv'foany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'foany = h`wv'foany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer from Other Relatives***
*wave 2 household amount transfers from other relatives
gen otamt1 = .
replace otamt1 = 0 if h`wv'foany == 0 | (mi(ft010_1) & inw`wv' == 1) | inrange(ft010_1,2,7) | inrange(ft010_1,10,22) | inrange(ft010_1,29,33)
replace otamt1 = ft011e_1 if (inlist(ft010_1,1,8,9) | inrange(ft010_1,23,28)) & inrange(ft011e_1,0,1000000)

gen otamt2 = .
replace otamt2 = 0 if h`wv'foany == 0 | (mi(ft010_2) & inw`wv' == 1) | inrange(ft010_2,2,7) | inrange(ft010_2,10,22) | inrange(ft010_2,29,33)
replace otamt2 = ft011e_2 if (inlist(ft010_2,1,8,9) | inrange(ft010_2,23,28)) & inrange(ft011e_2,0,1000000)

gen otamt3 = .
replace otamt3 = 0 if h`wv'foany == 0 | (mi(ft010_2) & inw`wv' == 1) | inrange(ft010_2,2,7) | inrange(ft010_2,10,22) | inrange(ft010_2,29,33)
replace otamt3 = ft011e_3 if (inlist(ft010_2,1,8,9) | inrange(ft010_3,23,28)) & inrange(ft011e_3,0,1000000)

gen h`wv'foamt = .
missing_financial ft011e_1 ft011e_2 ft011e_3, result(h`wv'foamt)
missing_h h`wv'foany, result(h`wv'foamt)
replace h`wv'foamt = otamt1 + otamt2 + otamt3 if !mi(otamt1) & !mi(otamt2) & !mi(otamt3)
label variable h`wv'foamt "h`wv'foamt:w`wv' amount transfer from other relatives"

bysort h`wv'coupid: replace h`wv'foamt = h`wv'foamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'foamt = h`wv'foamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop otamt1 otamt2 otamt3

***Any Transfers from Other Non-Relatives***
*wave 2 household any transfers from other non-relatives
gen h`wv'ffany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'ffany) wave(`wv')
replace h`wv'ffany = 0 if ft009_ == 5 | (ft009_ == 1 & (inrange(ft010_1,1,28) | inrange(ft010_1,1,28) | inrange(ft010_1,1,28)))		
replace h`wv'ffany = 1 if ft009_ == 1 & (inrange(ft010_1,29,33) | inrange(ft010_2,29,33) | inrange(ft010_3,29,33))
label variable h`wv'ffany "h`wv'ffany:w`wv' any transfers from other non-relatives"
label values h`wv'ffany trans

bysort h`wv'coupid: replace h`wv'ffany = h`wv'ffany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'ffany = h`wv'ffany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Amount Transfer from Other Non-Relatives***
*wave 2 household amount transfers from other non-relatives
gen ftamt1 = .
replace ftamt1 = 0 if h`wv'ffany == 0 | (mi(ft010_1) & inw`wv' == 1) | inrange(ft010_1,1,28) 
replace ftamt1 = ft011e_1 if inrange(ft010_1,29,33) & inrange(ft011e_1,0,1000000)

gen ftamt2 = .
replace ftamt2 = 0 if h`wv'ffany == 0 | (mi(ft010_2) & inw`wv' == 1) | inrange(ft010_2,1,28) 
replace ftamt2 = ft011e_2 if inrange(ft010_2,29,33) & inrange(ft011e_2,0,1000000)

gen ftamt3 = .
replace ftamt3 = 0 if h`wv'ffany == 0 | (mi(ft010_3) & inw`wv' == 1) | inrange(ft010_3,1,28) 
replace ftamt3 = ft011e_3 if inrange(ft010_3,29,33) & inrange(ft011e_3,0,1000000)

gen h`wv'ffamt = .
missing_financial ft011e_1 ft011e_2 ft011e_3 , result(h`wv'ffamt)
missing_h h`wv'ffany, result(h`wv'ffamt)
replace h`wv'ffamt = ftamt1 + ftamt2 + ftamt3 if !mi(ftamt1) & !mi(ftamt2) & !mi(ftamt3)
label variable h`wv'ffamt "h`wv'ffamt:w`wv' amount transfer from other non-relatives"

bysort h`wv'coupid: replace h`wv'ffamt = h`wv'ffamt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'ffamt = h`wv'ffamt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

drop ftamt1 ftamt2 ftamt3

***Total Amount Transfers Received***
*wave 2 household total amount transfers received
gen h`wv'frec = .
missing_h h`wv'fcamt h`wv'fpamt h`wv'foamt h`wv'ffamt, result(h`wv'frec)
replace h`wv'frec = h`wv'fcamt + h`wv'fpamt + h`wv'foamt + h`wv'ffamt if !mi(h`wv'fcamt) & !mi(h`wv'fpamt) & !mi(h`wv'foamt) & !mi(h`wv'ffamt)
label variable h`wv'frec "h`wv'frec:w`wv' total amount transfers received"

***Total Amount Transfers Given***
*wave 2 household total amount transfers given
gen h`wv'tgiv = .
missing_h h`wv'tcamt h`wv'tpamt h`wv'toamt h`wv'tfamt, result(h`wv'tgiv)
replace h`wv'tgiv = h`wv'tcamt + h`wv'tpamt + h`wv'toamt + h`wv'tfamt if !mi(h`wv'tcamt) & !mi(h`wv'tpamt) & !mi(h`wv'toamt) & !mi(h`wv'tfam)
label variable h`wv'tgiv "h`wv'tgiv:w`wv' total amount transfers given"

***Net Value Financial Transfers***
*wave 2 household net value financial transfers
gen h`wv'ftot = .
missing_h h`wv'fre h`wv'tgiv, result(h`wv'ftot)
replace h`wv'ftot = h`wv'frec - h`wv'tgiv if !mi(h`wv'frec) & !mi(h`wv'tgiv)
label variable h`wv'ftot "h`wv'ftot:w`wv' total net transfers"

***Any Weekly Social Activities***
*wave 2 respondent any weekly social activities
gen r`wv'socwk = .
missing_common ac002d1 ac002d2 ac002d3 ac002d4 ac002d5 ac002d6 ac002d7 ac002dno /// 
			ac003_1 ac003_2 ac003_3 ac003_4 ac003_5 ac003_6 ac003_7, result(r`wv'socwk) wave(`wv')
replace r`wv'socwk = 0 if ac002dno == 1 | (ac003_1 == 3 | ac003_2 == 3 | ac003_3 == 3 | ac003_4 == 3 | ac003_5 == 3 | ac003_6 == 3 | ac003_7 == 3)
replace r`wv'socwk = 1 if (ac002d1 == 1 & inlist(ac003_1,1,2)) | (ac002d2 == 1 & inlist(ac003_2,1,2)) | (ac002d3 == 1 & inlist(ac003_3,1,2)) | ///
		(ac002d4 == 1 & inlist(ac003_4,1,2)) |(ac002d5 == 1 & inlist(ac003_5,1,2)) |(ac002d6 == 1 & inlist(ac003_6,1,2)) |(ac002d7 == 1 & inlist(ac003_7,1,2))
label variable r`wv'socwk "r`wv'socwk:w`wv' r any weekly social activities"
label values r`wv'socwk wksoc

*wave 2 spouse any weekly social activites
gen s`wv'socwk = .
spouse r`wv'socwk, result(s`wv'socwk) wave(`wv')
label variable s`wv'socwk "s`wv'socwk:w`wv' s any weekly social activities"
label values s`wv'socwk wksoc

***Frequency Participate Religious Groups***
*wave 2 respondent frequency participate religious groups
gen r`wv'socrelg_s = .
missing_common ac002d6 ac002dno ac003_6, result(r`wv'socrelg_s) wave(`wv')
replace r`wv'socrelg_s = ac003_6 if inrange(ac003_6,1,3)
replace r`wv'socrelg_s = 4 if ac002d6 == 0 | ac002dno == 1
label variable r`wv'socrelg_s "r`wv'socrelg_s:w`wv' r freq participate religous groups"
label values r`wv'socrelg_s relfreq

*wave 2 spouse frequency participate religious groups
gen s`wv'socrelg_s = .
spouse r`wv'socrelg_s, result(s`wv'socrelg_s) wave(`wv')
label variable s`wv'socrelg_s "s`wv'socrelg_s:w`wv' s freq participate religous groups"
label values s`wv'socrelg_s relfreq

***Any Weekly Participation Religious Groups***
*wave 2 respondent any weekly religious groups
gen r`wv'relgwk = .
missing_common ac002d6 ac002dno ac003_6, result(r`wv'relgwk) wave(`wv')
replace r`wv'relgwk = 0 if ac002dno == 1 | ac002d6 == 0 | (ac002d6 == 1 & ac003_6 == 3)
replace r`wv'relgwk = 1 if ac002d6 == 1 & inlist(ac003_6,1,2)
label variable r`wv'relgwk "r`wv'relgwk:w`wv' r any weekly religious groups"
label values r`wv'relgwk wksoc

*wave 2 spouse any weekly religious groups
gen s`wv'relgwk = .
spouse r`wv'relgwk, result(s`wv'relgwk) wave(`wv')
label variable s`wv'relgwk "s`wv'relgwk:w`wv' s any weekly religious groups"
label values s`wv'relgwk wksoc


***drop wave 2 cover screen (respondent) file raw variables
drop `family_w2_cv_r'

***drop wave 2 Demographic file raw variables
drop `family_w2_dn'

***drop wave 2 Children file raw variables
drop `family_w2_ch'

***drop wave 2 imputation file raw variables***
drop `family_w2_imput'

***drop wave 2 sp file raw variables***
drop `family_w2_sp'

***drop wave 2 ft raw variables***
drop `family_w2_ft'

***drop wave 2 ac raw variables***
drop `family_w2_ac'

***drop wave 2 ph raw variables***
drop `family_w2_ph'

***drop wave 2 techvar raw variables***
drop `family_w2_techvar'

******************************************************************************************

*set wave number
local wv = 4
local prewv = 2

***merge with wave 4 cover screen (respondent) data***
local family_w4_cv_r hhsize gender cvresp 
merge 1:1 mergeid using "$wave_4_cv_r", keepusing(`family_w4_cv_r') nogen

***merge with wave 4 Demographic data***
local family_w4_dn dn034_ dn036_ dn037_ dn026_1 dn026_2 dn027_1 dn027_2 dn028_1 dn028_2 dn032_1 dn032_2 dn030_1 dn030_2
merge 1:1 mergeid using "$wave_4_dn", keepusing(`family_w4_dn') nogen

***merge with wave 4 Children data***
local family_w4_ch ch001_ ch021_ ch005_* ch007_* ch014_* ch524_ ch504_* ch524_ ch525d* ch526_* 
merge 1:1 mergeid using "$wave_4_ch", keepusing(`family_w4_ch') nogen

***merge with wave 4 imputation data***
local family_w4_imput nchild nchild_f ngrchild ngrchild_f fam_resp fin_resp
merge 1:1 mergeid using "$wave_4_imputations_", keepusing(`family_w4_imput') nogen

***merge with wave 4 cover screen data***
local family_w4_cv relrpers 
merge 1:1 mergeid using "$wave_4_cv_r", keepusing(`family_w4_cv') nogen

***merge with wave 4 sp data***
local family_w4_sp sp002_ sp003_1sp sp003_2sp sp003_3sp sp008_ sp009_1sp sp009_2sp sp009_3sp sp014_ sp018_ sp020_ ///
		sp019* sp021*
merge 1:1 mergeid using "$wave_4_sp", keepusing(`family_w4_sp') nogen

***merge with wave 4 ft data***
local family_w4_ft ft002_ ft003_* ft009_ ft010_* 
merge 1:1 mergeid using "$wave_4_ft", keepusing(`family_w4_ft') nogen

***merge with wave 4 ac data***
local family_w4_ac ac035d*
merge 1:1 mergeid using "$wave_4_ac", keepusing(`family_w4_ac') nogen

***merge with wave 4 physical health data***
local family_w4_ph ph048dno
merge 1:1 mergeid using "$wave_4_ph", keepusing(`family_w4_ph') nogen

***merge with wave 4 sn data***
local family_w4_sn sn005_* sn006_* sn007_*
merge 1:1 mergeid using "$wave_4_sn", keepusing(`family_w4_sn') nogen

***merge with wave 4 technical variables data***
local family_w4_techvar mn101_
merge 1:1 mergeid using "$wave_4_techvar", keepusing(`family_w4_techvar') nogen


***parental mortality: mother alive***
*wave 4 respondent mother alive status
tempvar mominhouse mominsn
egen `mominhouse' = total(relrpers == 5 & gender == 2), by(hhid4)
recode `mominhouse' (1/10 = 1)
gen `mominsn' = 0
replace `mominsn' = 1 if sn005_1 == 2 | sn005_2 == 2 | sn005_3 == 2 | sn005_4 == 2 | sn005_5 == 2 | sn005_6 == 2 | sn005_7 == 2

gen r4momliv = .
missing_common dn026_1, result(r4momliv)
replace r4momliv = .b if mn101_ == 0
replace r4momliv = r2momliv if inw4==1 & inlist(r2momliv,.d,.r,.m,0,1)
replace r4momliv = 0 if dn026_1 == 5 | ((r1momliv == 0 | r2momliv == 0) & inw4 == 1) 
replace r4momliv = 1 if ((dn026_1 == 1) | ///
                        (sn005_1 == 2 | sn005_2 == 2 | sn005_3 == 2 | sn005_4 == 2 | ///
                         sn005_5 == 2 | sn005_6 == 2 | sn005_7 == 2 ) | ///
                        (`mominhouse' == 1 & cvresp == 1))
label variable r4momliv "r4momliv:w4 r mother alive"

*wave 4 spouse mother alive status
gen s4momliv = .
spouse r4momliv, result(s4momliv) wave(4)
label variable s4momliv "s4momliv:w4 s mother alive"
label values r4momliv liv
label values s4momliv liv

***parental mortality: father alive***
*wave 4 respondent father alive status
tempvar dadinhouse dadinsn
egen `dadinhouse' = total(relrpers == 5 & gender == 1), by(hhid4)
recode `dadinhouse' (1/10 = 1)
gen `dadinsn' = 0
replace `dadinsn' = 1 if sn005_1 == 3 | sn005_2 == 3 | sn005_3 == 3 | sn005_4 == 3 | sn005_5 == 3 | sn005_6 == 3 | sn005_7 == 3

gen r4dadliv = .
missing_common dn026_2, result(r4dadliv)
replace r4dadliv = .b if mn101_ == 0
replace r4dadliv = r2dadliv if inw4==1 & inlist(r2dadliv,.d,.r,.m,0,1)
replace r4dadliv = 0 if dn026_2 == 5 | ((r1dadliv == 0 | r2dadliv == 0) & inw4 == 1)
replace r4dadliv = 1 if ((dn026_2 == 1) | ///
                        (sn005_1 == 3 | sn005_2 == 3 | sn005_3 == 3 | sn005_4 == 3 | ///
                         sn005_5 == 3 | sn005_6 == 3 | sn005_7 == 3 ) | ///
                        (`dadinhouse' == 1 & cvresp == 1))
label variable r4dadliv "r4dadliv:w4 r father alive"

*wave 4 spouse father alive status
gen s4dadliv = .
spouse r4dadliv, result(s4dadliv) wave(4)
label variable s4dadliv "s4dadliv:w4 s father alive"
label values r4dadliv liv
label values s4dadliv liv

***parental mortality: mother's current age or age at death***
*wave 4 respondent mother's age
gen r4momage = .
missing_common dn026_1, result(r4momage)
missing_common dn028_1 if dn026_1 == 1, result(r4momage)
missing_common dn027_1 if dn026_1 == 5, result(r4momage)
replace r4momage = .b if mn101_ == 0
replace r4momage = r1momage if inrange(r1momage,1,120) & r1momliv == 0 & inw4 == 1 
replace r4momage = r1momage + 6 if inrange(r1momage,1,120) & r1momliv == 1 & r4momliv == 1 & inw4 == 1
replace r4momage = r2momage if inrange(r2momage,1,120) & r2momliv == 0 & inw4 == 1 
replace r4momage = r2momage + 4 if inrange(r2momage,1,120) & r2momliv == 1 & r4momliv == 1 & inw4 == 1
replace r4momage = dn028_1 if dn026_1 == 1 & inrange(dn028_1, 1, 120)
replace r4momage = dn027_1 if dn026_1 == 5 & inrange(dn027_1, 1, 120) 
label variable r4momage "r4momage:w4 r mother age current/at death"

*wave 4 spouse mother's age
gen s4momage = .
spouse r4momage, result(s4momage) wave(4)
label variable s4momage "s4momage:w4 s mother age current/at death"

***parental mortality: father's current age or age at death***
*wave 4 respondent father's age
gen r4dadage = .
missing_common dn026_2, result(r4dadage)
missing_common dn028_2 if dn026_2 == 1, result(r4dadage)
missing_common dn027_2 if dn026_2 == 5, result(r4dadage)
replace r4dadage = .b if mn101_ == 0
replace r4dadage = r1dadage if inrange(r1dadage,1,120) & r1dadliv == 0 & inw4 == 1 
replace r4dadage = r1dadage + 6 if inrange(r1dadage,1,120) & r1dadliv == 1 & r4dadliv == 1 & inw4 == 1
replace r4dadage = r2dadage if inrange(r2dadage,1,120) & r2dadliv == 0 & inw4 == 1
replace r4dadage = r2dadage + 4 if inrange(r2dadage,1,120) & r2dadliv == 1 & r4dadliv == 1 & inw4 == 1
replace r4dadage = dn028_2 if dn026_2 == 1 & inrange(dn028_2, 1, 120)
replace r4dadage = dn027_2 if dn026_2 == 5 & inrange(dn027_2, 1, 120)
label variable r4dadage "r4dadage:w4 r father age current/at death"

*wave 4 spouse father's age
gen s4dadage = .
spouse r4dadage, result(s4dadage) wave(4)
label variable s4dadage "s4dadage:w4 s father age current/at death"

***Number of people living in house***
*wave 4 household number of people living in house
gen hh4hhres = hhsize if inw4==1
label variable hh4hhres "hh4hhres:w4 Number of people in HH"

***Number of children***
*wave 4 household number of living children
gen h`wv'child = nchild
label variable h`wv'child "h`wv'child:w`wv' Number of living children R/P"

*wave 4 couple-level flag number of living children
chimputation_flag nchild_f, gen(h`wv'fchild) by(h`wv'coupid)
label variable h`wv'fchild "h`wv'fchild:w`wv' Impute Flag Num of living children"
label values h`wv'fchild fchild

***Number of grandchildren***
*wave 4 household number of grandchildren
gen h`wv'grchild = ngrchild
label variable h`wv'grchild "h`wv'grchild:w`wv' Number of grandchildren R/P"

*wave 4 couple-level flag number of grandchildren
chimputation_flag ngrchild_f, gen(h`wv'fgrchild) by(h`wv'coupid)
label variable h`wv'fgrchild "h`wv'fgrchild:w`wv' Impute Flag Num of grandchildren"
label values h`wv'fgrchild fchild

***Number of living siblings***
*wave 4 respondent number of living brothers
gen r4livbro =.
missing_common dn036_, result(r4livbro)
replace r4livbro = r1livbro if !mi(r1livbro) & inw4==1
replace r4livbro = r2livbro if !mi(r2livbro) & inw4==1
replace r4livbro = 0 if dn034_ == 5
replace r4livbro = dn036_ if inrange(dn036_,0,20)
replace r4livbro = .i if inrange(dn036_,21,100)
label variable r4livbro "r4livbro:w4 r Number of living brothers"

*wave 4 respondent number of living sisters
gen r4livsis =.
missing_common dn037_, result(r4livsis)
replace r4livsis = r1livsis if !mi(r1livsis) & inw4==1
replace r4livsis = r2livsis if !mi(r2livsis) & inw4==1
replace r4livsis = 0 if dn034_ == 5
replace r4livsis = dn037_ if inrange(dn037_,0,20)
label variable r4livsis "r4livsis:w4 r Number of living sisters"

*wave 4 respondent number of living siblings
gen r4livsib =.
replace r4livsib = r4livbro + r4livsis
missing_h r4livbro r4livsis,result(r4livsib)
label variable r4livsib "r4livsib:w4 r Number of living siblings"

*wave 4 spouse number of living brothers
gen s4livbro =.
spouse r4livbro, result(s4livbro) wave(4)
label variable s4livbro "s4livbro:w4 s Number of living brothers"

*wave 4 spouse number of living sisters
gen s4livsis =.
spouse r4livsis, result(s4livsis) wave(4)
label variable s4livsis "s4livsis:w4 s Number of living sisters"

*wave 4 spouse number of living siblings
gen s4livsib =.
spouse r4livsib, result(s4livsib) wave(4)
label variable s4livsib "s4livsib:w4 s Number of living siblings"

***Number of living parents***
*wave 4 respondent number of living parents
gen r4livpar=.
missing_h r4momliv r4dadliv,result(r4livpar)
replace r4livpar = .b if mn101_ == 0
replace r4livpar = 0 if !missing(r4momliv) & !missing(r4dadliv)
replace r4livpar = r4livpar +1 if r4momliv==1 & !missing(r4dadliv)
replace r4livpar = r4livpar +1 if r4dadliv==1 & !missing(r4momliv)
label variable r4livpar "r4livpar:w4 r Number of living parents"

*wave 4 spouse number of living parents
gen s4livpar =.
spouse r4livpar, result(s4livpar) wave(4)
label variable s4livpar "s4livpar:w4 s Number of living parents"

***Number of living sons/daughters***
*wave 4 household number of living sons
recode ch005_* (-2 -1 2 = 0), prefix(m)
egen h`wv'son = rowtotal(mch005_*) if inw`wv' == 1,m 
replace h`wv'son = .m if (ch005_1==. | ch005_2==. | ch005_3==. | ch005_4==. | /// 
	ch005_5==. | ch005_6==. | ch005_7==. | ch005_8==. | ch005_9==. | ch005_10==. | ///
	ch005_11==. | ch005_12==. | ch005_13==. | ch005_14==. | ch005_15==. | ch005_16==. | ///
	ch005_17==. | ch005_18==. | ch005_19==. | ch005_20==.) & inlist(h`wv'son,.,0) & inw`wv' == 1
replace h`wv'son = .d if (ch005_1==-1 | ch005_2==-1 | ch005_3==-1 | ch005_4==-1 | /// 
	ch005_5==-1 | ch005_6==-1 | ch005_7==-1 | ch005_8==-1 | ch005_9==-1 | ch005_11==-1 | ///
	ch005_11==-1 | ch005_12==-1 | ch005_13==-1 | ch005_14==-1 | ch005_15==-1 | ch005_16==-1 | ///
	ch005_17==-1 | ch005_18==-1 | ch005_19==-1 | ch005_20==-1) & inlist(h`wv'son,.,.m,0)
replace h`wv'son = .r if (ch005_1==-2 | ch005_2==-2 | ch005_3==-2 | ch005_4==-2 | /// 
	ch005_5==-2 | ch005_6==-2 | ch005_7==-2 | ch005_8==-2 | ch005_9==-2 | ch005_11==-2 | ///
	ch005_11==-2 | ch005_12==-2 | ch005_13==-2 | ch005_14==-2 | ch005_15==-2 | ch005_16==-2 | ///
	ch005_17==-2 | ch005_18==-2 | ch005_19==-2 | ch005_20==-2) & inlist(h`wv'son,.,.m,0)
*wave 4 household number of living daughters
recode ch005_* (-2 -1 1 = 0) (2 = 1), prefix(f)
egen h`wv'dau = rowtotal(fch005_*) if inw`wv' == 1,m
replace h`wv'dau = .m if (ch005_1==. | ch005_2==. | ch005_3==. | ch005_4==. | /// 
	ch005_5==. | ch005_6==. | ch005_7==. | ch005_8==. | ch005_9==. | ch005_10==. | ///
	ch005_11==. | ch005_12==. | ch005_13==. | ch005_14==. | ch005_15==. | ch005_16==. | ///
	ch005_17==. | ch005_18==. | ch005_19==. | ch005_20==.) & inlist(h`wv'dau,.,0) & inw`wv' == 1
replace h`wv'dau = .d if (ch005_1==-1 | ch005_2==-1 | ch005_3==-1 | ch005_4==-1 | /// 
	ch005_5==-1 | ch005_6==-1 | ch005_7==-1 | ch005_8==-1 | ch005_9==-1 | ch005_11==-1 | ///
	ch005_11==-1 | ch005_12==-1 | ch005_13==-1 | ch005_14==-1 | ch005_15==-1 | ch005_16==-1 | ///
	ch005_17==-1 | ch005_18==-1 | ch005_19==-1 | ch005_20==-1) & inlist(h`wv'dau,.,.m,0)
replace h`wv'dau = .r if (ch005_1==-2 | ch005_2==-2 | ch005_3==-2 | ch005_4==-2 | /// 
	ch005_5==-2 | ch005_6==-2 | ch005_7==-2 | ch005_8==-2 | ch005_9==-2 | ch005_11==-2 | ///
	ch005_11==-2 | ch005_12==-2 | ch005_13==-2 | ch005_14==-2 | ch005_15==-2 | ch005_16==-2 | ///
	ch005_17==-2 | ch005_18==-2 | ch005_19==-2 | ch005_20==-2) & inlist(h`wv'dau,.,.m,0)
replace h`wv'son = 0 if h`wv'child == 0 | ((h`wv'child == h`wv'dau) & !mi(h`wv'dau))
replace h`wv'dau = 0 if h`wv'child == 0 | ((h`wv'child == h`wv'son) & !mi(h`wv'son))
label variable h`wv'son "h`wv'son:w`wv' Number of living sons R/P"
label variable h`wv'dau "h`wv'dau:w`wv' Number of living daughters R/P"

bysort h`wv'coupid: replace h`wv'son = h`wv'son[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'son = h`wv'son[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'dau = h`wv'dau[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'dau = h`wv'dau[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

drop mch005_* fch005_*

***Any Child Co-Resides***
*wave 4 household any child co-resides
gen h`wv'coresd = .
missing_common ch007_1 ch007_2 ch007_3 ch007_4 ch007_5 ch007_6 ch007_7 ch007_8 ch007_9 ch007_10 ch007_11 ch007_12 ch007_13 ch007_14 ///
	 ch007_15 ch007_16 ch007_17 ch007_18 ch007_19 ch007_20 ///
	 sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7 sn006_1 sn006_2 sn006_3 sn006_4 sn006_5 sn006_6 sn006_7 ///
	 ch526_1 ch526_2 ch526_3 ch526_4 ch526_5 ch526_6 ch526_7 ch526_8 ch526_9 ch526_10 ch526_11 ch526_12 ch526_13 ch526_14 ch526_15 ///
	 ch526_16 ch526_17 ch526_18 ch526_19 ch526_20 nchild mn101_ ch524_, result(h`wv'coresd)
replace h`wv'coresd = .k if nchild == 0
replace h`wv'coresd = h1coresd if inrange(h1coresd,0,1) & mn101_ == 1 & ch524_ == 5 & nchild != 0
replace h`wv'coresd = h2coresd if inrange(h2coresd,0,1) & mn101_ == 1 & ch524_ == 5 & nchild != 0
replace h`wv'coresd = 0 if (nchild != 0 & (inrange(ch007_1,2,9) | inrange(ch007_2,2,9) | inrange(ch007_3,2,9) | inrange(ch007_4,2,9) | /// 
                                    	   inrange(ch007_5,2,9) | inrange(ch007_6,2,9) | inrange(ch007_7,2,9) | inrange(ch007_8,2,9) | ///
                                    	   inrange(ch007_9,2,9) | inrange(ch007_10,2,9) | inrange(ch007_11,2,9) | inrange(ch007_12,2,9) | ///
                                    	   inrange(ch007_13,2,9) | inrange(ch007_14,2,9) | inrange(ch007_15,2,9) | inrange(ch007_16,2,9) | ///
                                    	   inrange(ch007_17,2,9) | inrange(ch007_18,2,9) | inrange(ch007_19,2,9) | inrange(ch007_20,2,9))) | ///
					(nchild != 0 & ((inrange(sn005_1,1,9) | inrange(sn005_1,14,96) | inrange(sn005_2,1,9) | inrange(sn005_2,14,96) | inrange(sn005_3,1,9) | ///
    								inrange(sn005_3,14,96) | inrange(sn005_4,1,9) | inrange(sn005_4,14,96) | inrange(sn005_5,1,9) | inrange(sn005_5,14,96) | ///
    								inrange(sn005_6,1,9) | inrange(sn005_6,14,96) | inrange(sn005_7,1,9) | inrange(sn005_7,14,96)) | ///
    								(inrange(sn006_1,2,9) | inrange(sn006_2,2,9) | inrange(sn006_3,2,9) | inrange(sn006_4,2,9) | inrange(sn006_5,2,9) | ///
    								inrange(sn006_6,2,9) | inrange(sn006_7,2,9)))) | ///                      	   
					(nchild != 0 & mn101_ == 1 & ch524_ == 1 & (inrange(ch526_1,2,8) | inrange(ch526_2,2,8) | inrange(ch526_3,2,8) | inrange(ch526_4,2,8) | ///
		                                            inrange(ch526_5,2,8) | inrange(ch526_6,2,8) | inrange(ch526_7,2,8) | inrange(ch526_8,2,8) | ///
		                                            inrange(ch526_9,2,8) | inrange(ch526_10,2,8) | inrange(ch526_11,2,8) | inrange(ch526_12,2,8) | ///
		                                            inrange(ch526_13,2,8) | inrange(ch526_14,2,8) | inrange(ch526_15,2,8) | inrange(ch526_16,2,8) | ///
		                                            inrange(ch526_17,2,8) | inrange(ch526_18,2,8) | inrange(ch526_19,2,8) | inrange(ch526_10,2,8)))
replace h`wv'coresd = 1 if (nchild != 0 & (ch007_1 == 1 | ch007_2 == 1 | ch007_3 == 1 | ch007_4 == 1 | ch007_5 == 1 | ch007_6 == 1 | ///
                                    	   ch007_7 == 1 | ch007_8 == 1 | ch007_9 == 1 | ch007_10 == 1 | ch007_11 == 1 | ch007_12 == 1 | ///
                                    	   ch007_13 == 1 | ch007_14 == 1 | ch007_15 == 1 | ch007_16 == 1 | ch007_17 == 1 | ch007_18 == 1 | ///
                                    	   ch007_19 == 1 | ch007_20 == 1)) | ///
    (nchild != 0 & ((inrange(sn005_1,10,13) & sn006_1 == 1) | (inrange(sn005_2,10,13) & sn006_2 == 1) | (inrange(sn005_3,10,13) & sn006_3 == 1) | ///
    					(inrange(sn005_4,10,13) & sn006_4 == 1) | (inrange(sn005_5,10,13) & sn006_5 == 1) | (inrange(sn005_6,10,13) & sn006_6 == 1) | ///
    					(inrange(sn005_7,10,13) & sn006_7 == 1))) | ///
		(nchild != 0 & mn101_ == 1 & ch524_ == 1 & (ch526_1 == 1 | ch526_2 == 1 | ch526_3 == 1 | ch526_4 == 1 | ch526_5 == 1 | ///
		                                            ch526_6 == 1 | ch526_7 == 1 | ch526_8 == 1 | ch526_9 == 1 | ch526_10 == 1 | ///
		                                            ch526_11 == 1 | ch526_12 == 1 | ch526_13 == 1 | ch526_14 == 1 | ch526_15 == 1 | ///
		                                            ch526_16 == 1 | ch526_17 == 1 | ch526_18 == 1 | ch526_19 == 1 | ch526_20 == 1))
label variable h`wv'coresd "h`wv'coresd:w`wv' any child coresides with R/P"
label values h`wv'coresd child

bysort h`wv'coupid: replace h`wv'coresd = h`wv'coresd[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'coresd = h`wv'coresd[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Child Lives Near***
*wave 4 household any child lives near
gen h`wv'lvnear = .
missing_common ch007_1 ch007_2 ch007_3 ch007_4 ch007_5 ch007_6 ch007_7 ch007_8 ch007_9 ch007_10 ch007_11 ch007_12 ch007_13 ch007_14 ///
	 sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7 sn006_1 sn006_2 sn006_3 sn006_4 sn006_5 sn006_6 sn006_7 ///
	 ch007_15 ch007_16 ch007_17 ch007_18 ch007_19 ch007_20 ch525d1 ch525d2 ch525d3 ch525d4 ch525d5 ch525d6 ch525d7 ch525d8 ///
	 ch525d9 ch525d10 ch525d11 ch525d12 ch525d13 ch525d14 ch525d15 ch525d16 ch525d17 ch525d18 ch525d19 ch525d20 ///
	 ch526_1 ch526_2 ch526_3 ch526_4 ch526_5 ch526_6 ch526_7 ch526_8 ch526_9 ch526_10 ch526_11 ch526_12 ch526_13 ch526_14 ch526_15 ///
	 ch526_16 ch526_17 ch526_18 ch526_19 ch526_20 nchild mn101_ ch524_, result(h`wv'lvnear)
replace h`wv'lvnear = .k if nchild == 0
replace h`wv'lvnear = h`prewv'lvnear if inrange(h`prewv'lvnear,0,1) & mn101_ == 1 & ch524_ == 5 & nchild != 0
replace h`wv'lvnear = 0 if (nchild != 0 & (inrange(ch007_1,6,9) | inrange(ch007_2,6,9) | inrange(ch007_3,6,9) | inrange(ch007_4,6,9) | /// 
			inrange(ch007_5,6,9) | inrange(ch007_6,6,9) | inrange(ch007_7,6,9) | inrange(ch007_8,6,9) | inrange(ch007_9,6,9) | ///
			inrange(ch007_10,6,9) | inrange(ch007_11,6,9) | inrange(ch007_12,6,9) | inrange(ch007_13,6,9) | inrange(ch007_14,6,9) | ///
			inrange(ch007_15,6,9) | inrange(ch007_16,6,9) | inrange(ch007_17,6,9) | inrange(ch007_18,6,9) | inrange(ch007_19,6,9) | inrange(ch007_20,6,9))) | ///
		(nchild != 0 & ((inrange(sn005_1,1,9) | inrange(sn005_1,14,96) | inrange(sn005_2,1,9) | inrange(sn005_2,14,96) | inrange(sn005_3,1,9) | ///
			inrange(sn005_3,14,96) | inrange(sn005_4,1,9) | inrange(sn005_4,14,96) | inrange(sn005_5,1,9) | inrange(sn005_5,14,96) | ///
			inrange(sn005_6,1,9) | inrange(sn005_6,1,9) | inrange(sn005_7,1,9) | inrange(sn005_7,1,9)) | inrange(sn006_1,6,9) | inrange(sn006_2,6,9) | ///
			inrange(sn006_3,6,9) | inrange(sn006_4,6,9) | inrange(sn006_5,6,9) | inrange(sn006_6,6,9) | inrange(sn006_7,6,9))) | ///
		(nchild != 0 & mn101_ == 1 & ch524_ == 1 & ((ch525d1==1 & inrange(ch526_1,6,8)) | (ch525d2==1 & inrange(ch526_2,6,8)) | ///
			(ch525d3==1 & inrange(ch526_3,6,8)) | (ch525d4==1 & inrange(ch526_4,6,8)) | (ch525d5==1 & inrange(ch526_5,6,8)) | ///
			(ch525d6==1 & inrange(ch526_6,6,8)) | (ch525d7==1 & inrange(ch526_7,6,8)) | (ch525d8==1 & inrange(ch526_8,6,8)) | ///
			(ch525d9==1 & inrange(ch526_9,6,8)) | (ch525d10==1 & inrange(ch526_10,6,8)) | (ch525d11==1 & inrange(ch526_11,6,8)) | ///
			(ch525d12==1 & inrange(ch526_12,6,8)) | (ch525d13==1 & inrange(ch526_13,6,8)) | (ch525d14==1 & inrange(ch526_14,6,8)) | ///
			(ch525d15==1 & inrange(ch526_15,6,8)) | (ch525d16==1 & inrange(ch526_16,6,8)) | (ch525d17==1 & inrange(ch526_17,6,8)) | ///
			(ch525d18==1 & inrange(ch526_18,6,8)) | (ch525d19==1 & inrange(ch526_19,6,8)) | (ch525d20==1 & inrange(ch526_20,6,8))))
replace h`wv'lvnear = 1 if (nchild != 0 & (inrange(ch007_1,1,5) | inrange(ch007_2,1,5) | inrange(ch007_3,1,5) | inrange(ch007_4,1,5) | /// 
			inrange(ch007_5,1,5) | inrange(ch007_6,1,5) | inrange(ch007_7,1,5) | inrange(ch007_8,1,5) | inrange(ch007_9,1,5) | ///
			inrange(ch007_10,1,5) | inrange(ch007_11,1,5) | inrange(ch007_12,1,5) | inrange(ch007_13,1,5) | inrange(ch007_14,1,5) | ///
			inrange(ch007_15,1,5) | inrange(ch007_16,1,5) | inrange(ch007_17,1,5) | inrange(ch007_18,1,5) | inrange(ch007_19,1,5) | inrange(ch007_20,1,5))) | ///
		(nchild != 0 & ((inrange(sn005_1,10,13) & inrange(sn006_1,1,5)) | (inrange(sn005_2,10,13) & inrange(sn006_2,1,5)) | ///
			(inrange(sn005_3,10,13) & inrange(sn006_3,1,5)) | (inrange(sn005_4,10,13) & inrange(sn006_4,1,5)) | (inrange(sn005_5,10,13) & inrange(sn006_5,1,5)) | ///
			(inrange(sn005_6,10,13) & inrange(sn006_6,1,5)) | (inrange(sn005_7,10,13) & inrange(sn006_7,1,5)))) | ///
		(nchild != 0 & mn101_ == 1 & ch524_ == 1 & ((ch525d1==1 & inrange(ch526_1,1,5)) | (ch525d2==1 & inrange(ch526_2,1,5)) | ///
			(ch525d3==1 & inrange(ch526_3,1,5)) | (ch525d4==1 & inrange(ch526_4,1,5)) | (ch525d5==1 & inrange(ch526_5,1,5)) | ///
			(ch525d6==1 & inrange(ch526_6,1,5)) | (ch525d7==1 & inrange(ch526_7,1,5)) | (ch525d8==1 & inrange(ch526_8,1,5)) | ///
			(ch525d9==1 & inrange(ch526_9,1,5)) | (ch525d10==1 & inrange(ch526_10,1,5)) | (ch525d11==1 & inrange(ch526_11,1,5)) | ///
			(ch525d12==1 & inrange(ch526_12,1,5)) | (ch525d13==1 & inrange(ch526_13,1,5)) | (ch525d14==1 & inrange(ch526_14,1,5)) | ///
			(ch525d15==1 & inrange(ch526_15,1,5)) | (ch525d16==1 & inrange(ch526_16,1,5)) | (ch525d17==1 & inrange(ch526_17,1,5)) | ///
			(ch525d18==1 & inrange(ch526_18,1,5)) | (ch525d19==1 & inrange(ch526_19,1,5)) | (ch525d20==1 & inrange(ch526_20,1,5))))
label variable h`wv'lvnear "h`wv'lvnear:w`wv' any child lives within 30 min"
label values h`wv'lvnear child

bysort h`wv'coupid: replace h`wv'lvnear = h`wv'lvnear[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'lvnear = h`wv'lvnear[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Weekly Contact with Children***
*wave 4 household any weekly contact with children
gen h`wv'kcnt = .
missing_common sn007_1 sn007_2 sn007_3 sn007_4 sn007_5 sn007_6 sn007_7 ch014_1 ch014_2 ch014_3 ch014_4 ch014_5 ch014_5 ch014_6 ///
ch014_7 ch014_8 ch014_9 ch014_10 ch014_11 ch014_12 ch014_13 ch014_14 ch014_15 ch014_16 ch014_17 ch014_18 ch014_19 ch014_20 nchild, result(h`wv'kcnt)
replace h`wv'kcnt = .d if h`wv'coresd == .d
replace h`wv'kcnt = .r if h`wv'coresd == .r
replace h`wv'kcnt = .k if nchild == 0
replace h`wv'kcnt = 0 if nchild != 0 & (inrange(sn007_1,4,7) | inrange(sn007_2,4,7) | inrange(sn007_3,4,7) | inrange(sn007_4,4,7) | ///
		inrange(sn007_5,4,7) | inrange(sn007_6,4,7) | inrange(sn007_7,4,7)) | (inrange(ch014_1,4,7) | inrange(ch014_2,4,7) | inrange(ch014_3,4,7) | ///
		inrange(ch014_4,4,7) | inrange(ch014_5,4,7) | inrange(ch014_6,4,7) | inrange(ch014_7,4,7) | inrange(ch014_8,4,7) | inrange(ch014_9,4,7) | ///
		inrange(ch014_10,4,7) | inrange(ch014_11,4,7) | inrange(ch014_12,4,7) | inrange(ch014_13,4,7) | inrange(ch014_14,4,7) | inrange(ch014_15,4,7) | ///
		inrange(ch014_16,4,7) | inrange(ch014_17,4,7) | inrange(ch014_18,4,7) | inrange(ch014_19,4,7) | inrange(ch014_20,4,7))
replace h`wv'kcnt = 1 if nchild != 0 & ((inrange(sn007_1,1,3) & inrange(sn005_1,10,13)) | (inrange(sn007_2,1,3) & inrange(sn005_2,10,13)) | /// 
		(inrange(sn007_3,1,3) & inrange(sn005_3,10,13)) | (inrange(sn007_4,1,3) & inrange(sn005_4,10,13)) | (inrange(sn007_5,1,3) & inrange(sn005_5,10,13)) | ///
		(inrange(sn007_6,1,3) & inrange(sn005_6,10,13)) | (inrange(sn007_7,1,3) & inrange(sn005_7,10,13)) | h`wv'coresd == 1) | ///
		(inrange(ch014_1,1,3) | inrange(ch014_2,1,3) | inrange(ch014_3,1,3) | inrange(ch014_4,1,3) | inrange(ch014_5,1,3) | inrange(ch014_6,1,3) | ///
		inrange(ch014_7,1,3) | inrange(ch014_8,1,3) | inrange(ch014_9,1,3) | inrange(ch014_10,1,3) | inrange(ch014_11,1,3) | inrange(ch014_12,1,3) | ///
		inrange(ch014_13,1,3) | inrange(ch014_14,1,3) | inrange(ch014_15,1,3) | inrange(ch014_16,1,3) | inrange(ch014_17,1,3) | inrange(ch014_18,1,3) | ///
		inrange(ch014_19,1,3) | inrange(ch014_20,1,3))
label variable h`wv'kcnt "h`wv'kcnt:w`wv' any weekly contact with children in person/phone/mail"
label values h`wv'kcnt contact

bysort h`wv'coupid: replace h`wv'kcnt = h`wv'kcnt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'kcnt = h`wv'kcnt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Frequency of Contact with Children***
*wave 4 household frequency contact with children
gen h`wv'kcntf = .
missing_common ch014_1 ch014_2 ch014_3 ch014_4 ch014_1 ch014_2 ch014_3 ch014_4 ch014_5 ch014_5 ch014_6 ch014_7 ///
 ch014_8 ch014_9 ch014_10 ch014_11 ch014_12 ch014_13 ch014_14 ch014_15 ch014_16 ch014_17 ch014_18 ch014_19 ch014_20 ///
 sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7 sn007_1 sn007_2 sn007_3 sn007_4 sn007_5 sn007_6 sn007_7 nchild, result(h`wv'kcntf)
replace h`wv'kcntf = .d if h`wv'coresd == .d
replace h`wv'kcntf = .r if h`wv'coresd == .r
replace h`wv'kcntf = .k if nchild == 0
replace h`wv'kcntf = 7 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 7) | (inrange(sn005_2,10,13) & sn007_2 == 7) | (inrange(sn005_3,10,13) & sn007_3 == 7) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 7) | (inrange(sn005_5,10,13) & sn007_5 == 7) | (inrange(sn005_6,10,13) & sn007_6 == 7) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 7)) | (ch014_1==7 | ch014_2==7 | ch014_3==7 | ch014_4==7 | ch014_5==7 | ch014_6==7 | ch014_7==7 | ///
		ch014_8==7 | ch014_9==7 | ch014_10==7 | ch014_11==7 | ch014_12==7 | ch014_13==7 | ch014_14==7 | ch014_15==7 | ch014_16==7 | ch014_17==7 | ///
		ch014_18==7 | ch014_19==7 | ch014_20==7)
replace h`wv'kcntf = 6 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 6) | (inrange(sn005_2,10,13) & sn007_2 == 6) | (inrange(sn005_3,10,13) & sn007_3 == 6) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 6) | (inrange(sn005_5,10,13) & sn007_5 == 6) | (inrange(sn005_6,10,13) & sn007_6 == 6) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 6)) | (ch014_1==6 | ch014_2==6 | ch014_3==6 | ch014_4==6 | ch014_5==6 | ch014_6==6 | ch014_7==6 | ///
		ch014_8==6 | ch014_9==6 | ch014_10==6 | ch014_11==6 | ch014_12==6 | ch014_13==6 | ch014_14==6 | ch014_15==6 | ch014_16==6 | ch014_17==6 | ///
		ch014_18==6 | ch014_19==6 | ch014_20==6)
replace h`wv'kcntf = 5 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 5) | (inrange(sn005_2,10,13) & sn007_2 == 5) | (inrange(sn005_3,10,13) & sn007_3 == 5) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 5) | (inrange(sn005_5,10,13) & sn007_5 == 5) | (inrange(sn005_6,10,13) & sn007_6 == 5) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 5)) | (ch014_1==5 | ch014_2==5 | ch014_3==5 | ch014_4==5 | ch014_5==5 | ch014_6==5 | ch014_7==5 | ///
		ch014_8==5 | ch014_9==5 | ch014_10==5 | ch014_11==5 | ch014_12==5 | ch014_13==5 | ch014_14==5 | ch014_15==5 | ch014_16==5 | ch014_17==5 | ///
		ch014_18==5 | ch014_19==5 | ch014_20==5)
replace h`wv'kcntf = 4 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 4) | (inrange(sn005_2,10,13) & sn007_2 == 4) | (inrange(sn005_3,10,13) & sn007_3 == 4) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 4) | (inrange(sn005_5,10,13) & sn007_5 == 4) | (inrange(sn005_6,10,13) & sn007_6 == 4) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 4)) | (ch014_1==4 | ch014_2==4 | ch014_3==4 | ch014_4==4 | ch014_5==4 | ch014_6==4 | ch014_7==4 | ///
		ch014_8==4 | ch014_9==4 | ch014_10==4 | ch014_11==4 | ch014_12==4 | ch014_13==4 | ch014_14==4 | ch014_15==4 | ch014_16==4 | ch014_17==4 | ///
		ch014_18==4 | ch014_19==4 | ch014_20==4)
replace h`wv'kcntf = 3 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 3) | (inrange(sn005_2,10,13) & sn007_2 == 3) | (inrange(sn005_3,10,13) & sn007_3 == 3) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 3) | (inrange(sn005_5,10,13) & sn007_5 == 3) | (inrange(sn005_6,10,13) & sn007_6 == 3) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 3)) | (ch014_1==3 | ch014_2==3 | ch014_3==3 | ch014_4==3 | ch014_5==3 | ch014_6==3 | ch014_7==3 | ///
		ch014_8==3 | ch014_9==3 | ch014_10==3 | ch014_11==3 | ch014_12==3 | ch014_13==3 | ch014_14==3 | ch014_15==3 | ch014_16==3 | ch014_17==3 | ///
		ch014_18==3 | ch014_19==3 | ch014_20==3)
replace h`wv'kcntf = 2 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 2) | (inrange(sn005_2,10,13) & sn007_2 == 2) | (inrange(sn005_3,10,13) & sn007_3 == 2) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 2) | (inrange(sn005_5,10,13) & sn007_5 == 2) | (inrange(sn005_6,10,13) & sn007_6 == 2) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 2)) | (ch014_1==2 | ch014_2==2 | ch014_3==2 | ch014_4==2 | ch014_5==2 | ch014_6==2 | ch014_7==2 | ///
		ch014_8==2 | ch014_9==2 | ch014_10==2 | ch014_11==2 | ch014_12==2 | ch014_13==2 | ch014_14==2 | ch014_15==2 | ch014_16==2 | ch014_17==2 | ///
		ch014_18==2 | ch014_19==2 | ch014_20==2)
replace h`wv'kcntf = 1 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 1) | (inrange(sn005_2,10,13) & sn007_2 == 1) | (inrange(sn005_3,10,13) & sn007_3 == 1) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 1) | (inrange(sn005_5,10,13) & sn007_5 == 1) | (inrange(sn005_6,10,13) & sn007_6 == 1) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 1) | h`wv'coresd == 1) | (ch014_1==1 | ch014_2==1 | ch014_3==1 | ch014_4==1 | ch014_5==1 | ch014_6==1 | ch014_7==1 | ///
		ch014_8==1 | ch014_9==1 | ch014_10==1 | ch014_11==1 | ch014_12==1 | ch014_13==1 | ch014_14==1 | ch014_15==1 | ch014_16==1 | ch014_17==1 | ///
		ch014_18==1 | ch014_19==1 | ch014_20==1)
label variable h`wv'kcntf "h`wv'kcntf:w`wv' frequency contact with children in person/phone/mail"
label values h`wv'kcntf freqcont

bysort h`wv'coupid: replace h`wv'kcntf = h`wv'kcntf[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'kcntf = h`wv'kcntf[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Weekly Contact with Parents***
*wave 4 respondent any weekly contact with parents
gen r`wv'pcnt = .
missing_common dn032_1 dn032_2 dn030_1 dn030_2 sn007_1 sn007_2 sn007_3 sn007_4 sn007_5 sn007_6 sn007_7, result(r`wv'pcnt)
replace r`wv'pcnt = .b if mn101_ == 0
replace r`wv'pcnt = .n if r`wv'livpar == 0 
replace r`wv'pcnt = 0 if r`wv'livpar != 0 & (inrange(dn032_1,4,7) | inrange(dn032_2,4,7) | inrange(sn007_1,4,7) | inrange(sn007_2,4,7) | inrange(sn007_3,4,7) | ///
		inrange(sn007_4,4,7) | inrange(sn007_5,4,7) | inrange(sn007_6,4,7) | inrange(sn007_7,4,7))
replace r`wv'pcnt = 1 if r`wv'livpar != 0 & (inrange(dn032_1,1,3) | inrange(dn032_2,1,3) | dn030_1 == 1 | dn030_2 == 1 | (inrange(sn005_1,2,7) & inrange(sn007_1,1,3)) | ///
		(inrange(sn005_2,2,7) & inrange(sn007_2,1,3)) | (inrange(sn005_3,2,7) & inrange(sn007_3,1,3)) | (inrange(sn005_4,2,7) & inrange(sn007_4,1,3)) | ///
		(inrange(sn005_5,2,7) & inrange(sn007_5,1,3)) | (inrange(sn005_6,2,7) & inrange(sn007_6,1,3)) | (inrange(sn005_7,2,7) & inrange(sn007_7,1,3)))  | dn030_1 == 1 | dn030_2 == 1
label variable r`wv'pcnt "r`wv'pcnt:w`wv' r any weekly contact with parents in person/phone/mail"
label values r`wv'pcnt contact

*wave 4 spouse any weekly contact with parents
gen s`wv'pcnt = .
spouse r`wv'pcnt, result(s`wv'pcnt) wave(`wv')
label variable s`wv'pcnt "s`wv'pcnt:w`wv' s any weekly contact with parents in person/phone/mail"
label values s`wv'pcnt contact

***Frequency of Contact with Parents***
*wave 4 respondent frequency contact with parents
gen r`wv'pcntf = .
missing_common dn032_1 dn032_2 dn030_1 dn030_2 sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7 ///
		sn007_1 sn007_2 sn007_3 sn007_4 sn007_5 sn007_6 sn007_7, result(r`wv'pcntf)
replace r`wv'pcntf = .b if mn101_ == 0
replace r`wv'pcntf = .n if r`wv'livpar == 0
replace r`wv'pcntf = 7 if r`wv'livpar != 0 & (dn032_1 == 7 | dn032_2 == 7 | (inrange(sn005_1,2,7) & sn007_1 == 7) |  (inrange(sn005_2,2,7) & sn007_2 == 7) | ///
		(inrange(sn005_3,2,7) & sn007_3 == 7) | (inrange(sn005_4,2,7) & sn007_4 == 7) | (inrange(sn005_5,2,7) & sn007_5 == 7) | ///
		(inrange(sn005_6,2,7) & sn007_6 == 7) | (inrange(sn005_7,2,7) & sn007_7 == 7))
replace r`wv'pcntf = 6 if r`wv'livpar != 0 & (dn032_1 == 6 | dn032_2 == 6 | (inrange(sn005_1,2,7) & sn007_1 == 6) |  (inrange(sn005_2,2,7) & sn007_2 == 6) | ///
		(inrange(sn005_3,2,7) & sn007_3 == 6) | (inrange(sn005_4,2,7) & sn007_4 == 6) | (inrange(sn005_5,2,7) & sn007_5 == 6) | ///
		(inrange(sn005_6,2,7) & sn007_6 == 6) | (inrange(sn005_7,2,7) & sn007_7 == 6))
replace r`wv'pcntf = 5 if r`wv'livpar != 0 & (dn032_1 == 5 | dn032_2 == 5 | (inrange(sn005_1,2,7) & sn007_1 == 5) |  (inrange(sn005_2,2,7) & sn007_2 == 5) | ///
		(inrange(sn005_3,2,7) & sn007_3 == 5) | (inrange(sn005_4,2,7) & sn007_4 == 5) | (inrange(sn005_5,2,7) & sn007_5 == 5) | ///
		(inrange(sn005_6,2,7) & sn007_6 == 5) | (inrange(sn005_7,2,7) & sn007_7 == 5))
replace r`wv'pcntf = 4 if r`wv'livpar != 0 & (dn032_1 == 4 | dn032_2 == 4 | (inrange(sn005_1,2,7) & sn007_1 == 4) |  (inrange(sn005_2,2,7) & sn007_2 == 4) | ///
		(inrange(sn005_3,2,7) & sn007_3 == 4) | (inrange(sn005_4,2,7) & sn007_4 == 4) | (inrange(sn005_5,2,7) & sn007_5 == 4) | ///
		(inrange(sn005_6,2,7) & sn007_6 == 4) | (inrange(sn005_7,2,7) & sn007_7 == 4))
replace r`wv'pcntf = 3 if r`wv'livpar != 0 & (dn032_1 == 3 | dn032_2 == 3 | (inrange(sn005_1,2,7) & sn007_1 == 3) |  (inrange(sn005_2,2,7) & sn007_2 == 3) | ///
		(inrange(sn005_3,2,7) & sn007_3 == 3) | (inrange(sn005_4,2,7) & sn007_4 == 3) | (inrange(sn005_5,2,7) & sn007_5 == 3) | ///
		(inrange(sn005_6,2,7) & sn007_6 == 3) | (inrange(sn005_7,2,7) & sn007_7 == 3))
replace r`wv'pcntf = 2 if r`wv'livpar != 0 & (dn032_1 == 2 | dn032_2 == 2 | (inrange(sn005_1,2,7) & sn007_1 == 2) |  (inrange(sn005_2,2,7) & sn007_2 == 2) | ///
		(inrange(sn005_3,2,7) & sn007_3 == 2) | (inrange(sn005_4,2,7) & sn007_4 == 2) | (inrange(sn005_5,2,7) & sn007_5 == 2) | ///
		(inrange(sn005_6,2,7) & sn007_6 == 2) | (inrange(sn005_7,2,7) & sn007_7 == 2))
replace r`wv'pcntf = 1 if r`wv'livpar != 0 & (dn032_1 == 1 | dn032_2 == 1 | dn030_1 == 1 | dn030_2 == 1 | (inrange(sn005_1,2,7) & sn007_1 == 1) | ///
		(inrange(sn005_2,2,7) & sn007_2 == 1) | (inrange(sn005_3,2,7) & sn007_3 == 1) | (inrange(sn005_4,2,7) & sn007_4 == 1) | ///
		(inrange(sn005_5,2,7) & sn007_5 == 1) | (inrange(sn005_6,2,7) & sn007_6 == 1) | (inrange(sn005_7,2,7) & sn007_7 == 1))
label variable r`wv'pcntf "r`wv'pcntf:w`wv' r frequency contact with parents in person/phone/mail"
label values r`wv'pcntf freqcont

*wave 4 spouse frequency contact with parents
gen s`wv'pcntf = .
spouse r`wv'pcntf, result(s`wv'pcntf) wave(`wv')
label variable s`wv'pcntf "s`wv'pcntf:w`wv' s frequency contact with parents in person/phone/mail"
label values s`wv'pcntf freqcont

***Received Any Informal Care***
*wave 4 household received any informal care
gen hout = .
missing_common sp002_, result(hout) wave(`wv')
replace hout = 0 if sp002_ == 5
replace hout = 1 if sp002_ == 1
bysort h`wv'coupid: replace hout = hout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace hout = hout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rin = .
missing_common sp020_ hhsize ph048dno, result(rin) wave(`wv')
replace rin = 0 if sp020_ == 5 | hhsize == 1 | ph048dno == 1
replace rin = 1 if sp020_ == 1

gen spin = .
spouse rin, result(spin) wave(`wv')

gen h`wv'raany = .
replace h`wv'raany = .b if mi(spin) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .m if (hout == .m | rin == .m | spin == .m | r`wv'mstat == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .m if (hout == .m | rin == .m | r`wv'mstat == .m) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = .r if (hout == .r | rin == .r | spin == .r | r`wv'mstat == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .r if (hout == .r | rin == .r | r`wv'mstat == .r) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = .d if (hout == .d | rin == .d | spin == .d | r`wv'mstat == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .d if (hout == .d | rin == .d | r`wv'mstat == .d) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = 0 if (hout == 0 & rin == 0 & spin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = 0 if (hout == 0 & rin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = 1 if (hout == 1 | rin == 1 | spin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = 1 if (hout == 1 | rin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
label variable h`wv'raany "h`wv'raany:w`wv' received any informal care"
label values h`wv'raany carer

drop hout rin spin

***Received Informal Care from Children***
*wave 4 household received informal care from children
gen chout = .
missing_common sp002_ sp003_1sp sp003_2sp sp003_3sp sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7 nchild, result(chout) wave(`wv')
replace chout = .k if nchild == 0
replace chout = .x if sp003_1sp==96 | sp003_2sp==96 | sp003_3sp==96
replace chout = 0 if nchild != 0 & (sp002_ == 5 | (sp002_ == 1 & ((inrange(sp003_1sp,1,9) | inrange(sp003_1sp,22,33) | inrange(sp003_1sp,35,96)) | /// 
		(inrange(sp003_2sp,1,9) | inrange(sp003_2sp,22,33) | inrange(sp003_2sp,35,96)) | (inrange(sp003_3sp,1,9) | inrange(sp003_3sp,22,33) | ///
		inrange(sp003_3sp,35,96))) | ((inrange(sp003_1sp,101,107) | inrange(sp003_2sp,101,107) | inrange(sp003_3sp,101,107)) & (!inrange(sn005_1,10,13) | ///
		!inrange(sn005_2,10,13) | !inrange(sn005_3,10,13) | !inrange(sn005_4,10,13) | !inrange(sn005_5,10,13) | !inrange(sn005_6,10,13) | ///
		!inrange(sn005_7,10,13)))))
replace chout = 1 if nchild != 0 & (sp002_ == 1 & ((inlist(sp003_1sp,19,20,21,34) | inlist(sp003_2sp,19,20,21,34) | inlist(sp003_3sp,19,20,21,34)) | ///
		((inrange(sp003_1,101,107) | inrange(sp003_2,101,107) | inrange(sp003_3,101,107)) & (inrange(sn005_1,10,13) | inrange(sn005_2,10,13) | ///
		inrange(sn005_3,10,13) | inrange(sn005_4,10,13) | inrange(sn005_5,10,13) | inrange(sn005_6,10,13) | inrange(sn005_7,10,13)))))
bysort h`wv'coupid: replace chout = chout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace chout = chout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen crin = .
missing_common sp020_ sp021d19sp sp021d20sp sp021d21sp sp021d34sp sp021d1sn sp021d2sn sp021d3sn sp021d4sn sp021d5sn sp021d6sn sp021d7sn hhsize ph048dno nchild, result(crin) wave(`wv')
replace crin = .k if nchild == 0
replace crin = 0 if hhsize == 1 | ph048dno == 1 | (nchild != 0 & (sp020_ == 5 | (sp020_ == 1 & ((sp021d19sp != 1 | sp021d20sp != 1 | sp021d21sp != 1 | sp021d34sp != 1) | ///
		((sp021d1sn != 1 | sp021d2sn != 1 | sp021d3sn != 1 | sp021d4sn != 1 | sp021d5sn != 1 | sp021d6sn != 1 | sp021d7sn != 1) & (!inrange(sn005_1,10,13) | ///
		!inrange(sn005_2,10,13) | !inrange(sn005_3,10,13) | !inrange(sn005_4,10,13) | !inrange(sn005_5,10,13) | !inrange(sn005_6,10,13) | ///
		!inrange(sn005_7,10,13)))))))
replace crin = 1 if nchild != 0 & sp020_ == 1 & ((sp021d19sp == 1 | sp021d20sp == 1 | sp021d21sp == 1 | sp021d34sp == 1) | ///
		((sp021d1sn == 1 | sp021d2sn == 1 | sp021d3sn == 1 | sp021d4sn == 1 | sp021d5sn == 1 | sp021d6sn == 1 | sp021d7sn == 1) & (inrange(sn005_1,10,13) | ///
		inrange(sn005_2,10,13) | inrange(sn005_3,10,13) | inrange(sn005_4,10,13) | inrange(sn005_5,10,13) | inrange(sn005_6,10,13) | ///
		inrange(sn005_7,10,13))))

gen cspin = .
spouse crin, result(cspin) wave (`wv')

gen h`wv'rccare = .
replace h`wv'rccare = .b if mi(cspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .m if (chout == .m | crin == .m | cspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .m if (chout == .m | crin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .r if (chout == .r | crin == .r | cspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .r if (chout == .r | crin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .d if (chout == .d | crin == .d | cspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .d if (chout == .d | crin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .k if (chout == .k | crin == .k | cspin == .k) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .k if (chout == .k | crin == .k) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .x if (chout == .x | crin == .x | cspin == .x) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .x if (chout == .x | crin == .x) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = 0 if (chout == 0 & crin == 0 & cspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = 0 if (chout == 0 & crin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = 1 if (chout == 1 | crin == 1 | cspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = 1 if (chout == 1 | crin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
label variable h`wv'rccare "h`wv'rccare:w`wv' received informal care from children"
label values h`wv'rccare carer

drop chout crin cspin

***Received Informal Care from Relatives***
*wave 4 household received informal care from relatives
gen rhout = .
missing_common sp002_ sp003_1sp sp003_2sp sp003_3sp, result(rhout) wave(`wv')
replace rhout = .x if sp003_1sp==96 | sp003_2sp==96 | sp003_3sp==96
replace rhout = 0 if sp002_ == 5 | (sp002_ == 1 & ((inrange(sp003_1sp,10,21) | inrange(sp003_1sp,29,96) | inrange(sp003_2sp,10,21) | ///
		inrange(sp003_2sp,29,96) | inrange(sp003_3sp,10,21) | inrange(sp003_3sp,29,96)) | ((inrange(sp003_1sp,101,107) | inrange(sp003_2sp,101,107) | ///
		inrange(sp003_3sp,101,107)) & (inrange(sn005_1,10,13) | inrange(sn005_1,21,96) | inrange(sn005_2,10,13) | inrange(sn005_2,21,96) | ///
		inrange(sn005_3,10,13) | inrange(sn005_3,21,96) | inrange(sn005_4,10,13) | inrange(sn005_4,21,96) | inrange(sn005_5,10,13) | ///
		inrange(sn005_5,21,96) | inrange(sn005_6,10,13) | inrange(sn005_6,21,96) | inrange(sn005_7,10,13) | inrange(sn005_7,21,96)))))
replace rhout = 1 if sp002_ == 1 & ((inrange(sp003_1sp,1,9) | inrange(sp003_1sp,22,28) | inrange(sp003_2sp,1,9) | inrange(sp003_2sp,22,28) | ///
		inrange(sp003_3sp,1,9) | inrange(sp003_3sp,22,28)) | ((inrange(sp003_1sp,101,107) | inrange(sp003_2sp,101,107) | inrange(sp003_3sp,101,107)) & ///
		(inrange(sn005_1,1,9) | inrange(sn005_1,14,20) | inrange(sn005_2,1,9) | inrange(sn005_2,14,20) | inrange(sn005_3,1,9) | inrange(sn005_3,14,20) | ///
		inrange(sn005_4,1,9) | inrange(sn005_4,14,20) | inrange(sn005_5,1,9) | inrange(sn005_5,14,20) | inrange(sn005_6,1,9) | inrange(sn005_6,14,20) | ///
		inrange(sn005_7,1,9) | inrange(sn005_7,14,20))))
bysort h`wv'coupid: replace rhout = rhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace rhout = rhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rrin = .
missing_common sp020_ sp021d1sp sp021d2sp sp021d3sp sp021d4sp sp021d5sp sp021d6sp sp021d7sp sp021d8sp sp021d9sp sp021d22sp sp021d23sp sp021d24sp sp021d25sp ///
		sp021d26sp sp021d27sp sp021d28sp hhsize ph048dno, result(rrin) wave(`wv')
replace rrin = 0 if hhsize == 1 | ph048dno == 1 | (sp020_ == 5 | (sp020_ == 1 & ((sp021d1sp != 1 | sp021d2sp != 1 | sp021d3sp != 1 | sp021d4sp != 1 | ///
		sp021d5sp != 1 | sp021d6sp != 1 | sp021d7sp != 1 | sp021d8sp != 1 | sp021d9sp != 1 | sp021d22sp != 1 | sp021d23sp != 1 | sp021d24sp != 1 | ///
		sp021d25sp != 1 | sp021d26sp != 1 | sp021d27sp != 1 | sp021d28sp != 1) | ((sp021d1sn != 1 | sp021d2sn != 1 | sp021d3sn != 1 | sp021d4sn != 1 | ///
		sp021d5sn != 1 | sp021d6sn != 1 | sp021d7sn !=1) & (inrange(sn005_1,10,13) | inrange(sn005_1,21,96) | inrange(sn005_2,10,13) | inrange(sn005_2,21,96) | ///
		inrange(sn005_3,10,13) | inrange(sn005_3,21,96) | inrange(sn005_4,10,13) | inrange(sn005_4,21,96) | inrange(sn005_5,10,13) | ///
		inrange(sn005_5,21,96) | inrange(sn005_6,10,13) | inrange(sn005_6,21,96) | inrange(sn005_7,10,13) | inrange(sn005_7,21,96))))))
replace rrin = 1 if sp020_ == 1 & ((sp021d1sp == 1 | sp021d2sp == 1 | sp021d3sp == 1 | sp021d4sp == 1 | ///
		sp021d5sp == 1 | sp021d6sp == 1 | sp021d7sp == 1 | sp021d8sp == 1 | sp021d9sp == 1 | sp021d22sp == 1 | sp021d23sp == 1 | sp021d24sp == 1 | ///
		sp021d25sp == 1 | sp021d26sp == 1 | sp021d27sp == 1 | sp021d28sp == 1) | ((((sp021d1sn == 1 | sp021d2sn == 1 | sp021d3sn == 1 | sp021d4sn == 1 | ///
		sp021d5sn == 1 | sp021d6sn == 1 | sp021d7sn ==1) & (inrange(sn005_1,1,9) | inrange(sn005_1,14,20) | inrange(sn005_2,1,9) | inrange(sn005_2,14,20) | inrange(sn005_3,1,9) | inrange(sn005_3,14,20) | ///
		inrange(sn005_4,1,9) | inrange(sn005_4,14,20) | inrange(sn005_5,1,9) | inrange(sn005_5,14,20) | inrange(sn005_6,1,9) | inrange(sn005_6,14,20) | ///
		inrange(sn005_7,1,9) | inrange(sn005_7,14,20))))))

gen rspin = .
spouse rrin, result(rspin) wave (`wv')

gen h`wv'rrcare = .
replace h`wv'rrcare = .b if mi(rspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .m if (rhout == .m | rrin == .m | rspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .m if (rhout == .m | rrin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = .r if (rhout == .r | rrin == .r | rspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .r if (rhout == .r | rrin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = .d if (rhout == .d | rrin == .d | rspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .d if (rhout == .d | rrin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = .x if (rhout == .x | rrin == .x | rspin == .x) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .x if (rhout == .x | rrin == .x) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = 0 if (rhout == 0 & rrin == 0 & rspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = 0 if (rhout == 0 & rrin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = 1 if (rhout == 1 | rrin == 1 | rspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = 1 if (rhout == 1 | rrin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
label variable h`wv'rrcare "h`wv'rrcare:w`wv' received informal care from relatives"
label values h`wv'rrcare carer

drop rhout rrin rspin

***Received Informal Care from non-relatives***
*wave 4 household received informal care from non-relatives
gen fhout = .
missing_common sp002_ sp003_1sp sp003_2sp sp003_3sp, result(fhout) wave(`wv')
replace fhout = .x if sp003_1sp==96 | sp003_2sp==96 | sp003_3sp==96
replace fhout = 0 if sp002_ == 5 | (sp002_ == 1 & ((inrange(sp003_1sp,1,28) | inlist(sp003_1sp,34,96) | inrange(sp003_2sp,1,28) | inlist(sp003_2sp,34,96) | ///
		inrange(sp003_3sp,1,28) | inlist(sp003_3sp,34,96)) | ((inrange(sp003_1sp,101,107) | inrange(sp003_2sp,101,107) | inrange(sp003_3sp,101,107) & ///
		(inrange(sn005_1,1,20) | inrange(sn005_2,1,20) | inrange(sn005_3,1,20) | inrange(sn005_4,1,20) | inrange(sn005_5,1,20) | inrange(sn005_6,1,20) | ///
		inrange(sn005_7,1,20))))))
replace fhout = 1 if sp002_ == 1 & ((inrange(sp003_1sp,29,33) | inrange(sp003_1sp,35,37) | inrange(sp003_2sp,29,33) | inrange(sp003_2sp,35,37) | ///
		inrange(sp003_3sp,29,33) | inrange(sp003_3sp,35,37)) | ((inrange(sp003_1sp,101,107) | inrange(sp003_2sp,101,107) | inrange(sp003_3sp,101,107)) & ///
		(inrange(sn005_1,21,27) | inrange(sn005_2,21,27) | inrange(sn005_3,21,27) | inrange(sn005_4,21,27) | inrange(sn005_5,21,27) | ///
		inrange(sn005_6,21,27) | inrange(sn005_7,21,27))))
bysort h`wv'coupid: replace fhout = fhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace fhout = fhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen frin = .
missing_common sp020_ sp021d29sp sp021d30sp sp021d31sp sp021d32sp sp021d35sp sp021d36sp sp021d37sp hhsize ph048dno, result(frin) wave(`wv')
replace frin = 0 if hhsize == 1 | ph048dno == 1 | (sp020_ == 5 | (sp020_ == 1 & ((sp021d29sp != 1 | sp021d30sp != 1 | sp021d31sp != 1 | sp021d32sp != 1 | ///
		sp021d35sp != 1 | sp021d36sp != 1 | sp021d37sp != 1) | ((sp021d1sn != 1 | sp021d2sn != 1 | sp021d3sn != 1 | sp021d4sn != 1 | sp021d5sn != 1 | ///
		sp021d6sn != 1 | sp021d7sn != 1) & (!inrange(sn005_1,21,27) | !inrange(sn005_2,21,27) | !inrange(sn005_3,21,27) | !inrange(sn005_4,21,27) | !inrange(sn005_5,21,27) | ///
		!inrange(sn005_6,21,27) | !inrange(sn005_7,21,27))))))
replace frin = 1 if sp020_ == 1 & ((sp021d29sp == 1 | sp021d30sp == 1 | sp021d31sp == 1 | sp021d32sp == 1 | ///
		sp021d35sp == 1 | sp021d36sp == 1 | sp021d37sp == 1) | ((sp021d1sn == 1 | sp021d2sn == 1 | sp021d3sn == 1 | sp021d4sn == 1 | sp021d5sn == 1 | ///
		sp021d6sn == 1 | sp021d7sn == 1) & (inrange(sn005_1,21,27) | inrange(sn005_2,21,27) | inrange(sn005_3,21,27) | inrange(sn005_4,21,27) | inrange(sn005_5,21,27) | ///
		inrange(sn005_6,21,27) | inrange(sn005_7,21,27))))

gen fspin = .
spouse frin, result(fspin) wave (`wv')

gen h`wv'rfcare = .
replace h`wv'rfcare = .b if mi(fspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .m if (fhout == .m | frin == .m | fspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .m if (fhout == .m | frin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = .r if (fhout == .r | frin == .r | fspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .r if (fhout == .r | frin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = .d if (fhout == .d | frin == .d | fspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .d if (fhout == .d | frin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = .x if (fhout == .x | frin == .x | fspin == .x) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .x if (fhout == .x | frin == .x) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = 0 if (fhout == 0 & frin == 0 & fspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = 0 if (fhout == 0 & frin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = 1 if (fhout == 1 | frin == 1 | fspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = 1 if (fhout == 1 | frin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
label variable h`wv'rfcare "h`wv'rfcare:w`wv' received informal care from non-relatives"
label values h`wv'rfcare carer

drop fhout frin fspin

***Gave Any Informal Care***
*wave 4 respondent gave any informal care 
gen hout = .
missing_common sp008_, result(hout) wave(`wv')
replace hout = 0 if sp008_ == 5
replace hout = 1 if sp008_ == 1
bysort h`wv'coupid: replace hout = hout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace hout = hout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rin = .
missing_common sp018_ hhsize, result(rin) wave(`wv')
replace rin = 0 if sp018_ == 5 | hhsize == 1
replace rin = 1 if sp018_ == 1

gen spin = .
spouse rin, result(spin) wave(`wv')

gen h`wv'gcaany = .
replace h`wv'gcaany = .b if mi(spin) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .m if (hout == .m | rin == .m | spin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .m if (hout == .m | rin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = .r if (hout == .r | rin == .r | spin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .r if (hout == .r | rin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = .d if (hout == .d | rin == .d | spin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .d if (hout == .d | rin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = 0 if (hout == 0 & rin == 0 & spin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = 0 if (hout == 0 & rin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = 1 if (hout == 1 | rin == 1 | spin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = 1 if (hout == 1 | rin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
label variable h`wv'gcaany "h`wv'gcaany:w`wv' gave any informal care"
label values h`wv'gcaany careg

drop hout rin spin

***Gave Informal Care to Children***
*wave 4 respondent gave informal care to children
gen chout = .
missing_common sp008_ sp009_1sp sp009_2sp sp009_3sp nchild, result(chout) wave(`wv')
replace chout = .k if nchild == 0
replace chout = .x if sp009_1sp==96 | sp009_2sp==96 | sp009_3sp==96
replace chout = 0 if nchild != 0 & (sp008_ == 5 | (sp008_ == 1 & ((inrange(sp009_1sp,1,9) | inrange(sp009_1sp,22,32) | inrange(sp009_1sp,35,96) | ///
		inrange(sp009_2sp,1,9) | inrange(sp009_2sp,22,32) | inrange(sp009_2sp,35,96) | inrange(sp009_3sp,1,9) | inrange(sp009_3sp,22,32) | inrange(sp009_3sp,35,96)) | ///
		((inrange(sp009_1sp,101,107) | inrange(sp009_2sp,101,107) | inrange(sp009_3sp,101,107)) & (inrange(sn005_1,1,9) | inrange(sn005_1,14,96) | ///
		inrange(sn005_2,1,9) | inrange(sn005_2,14,96) | inrange(sn005_3,1,9) | inrange(sn005_3,14,96) | inrange(sn005_4,1,9) | inrange(sn005_4,14,96) | ///
		inrange(sn005_5,1,9) | inrange(sn005_5,14,96) | inrange(sn005_6,1,9) | inrange(sn005_6,14,96) | inrange(sn005_7,1,9) | inrange(sn005_7,14,96))))))
replace chout = 1 if nchild != 0 & sp008_ == 1 & ((inlist(sp009_1sp,19,20,21,34) | inlist(sp009_2sp,19,20,21,34) | inlist(sp009_3sp,19,20,21,34)) | ///
		((inrange(sp009_1sp,101,107) | inrange(sp009_2sp,101,107) | inrange(sp009_3sp,101,107)) & (inrange(sn005_1,10,13) | inrange(sn005_2,10,13) | ///
		inrange(sn005_3,10,13) | inrange(sn005_4,10,13) | inrange(sn005_5,10,13) | inrange(sn005_6,10,13) | inrange(sn005_7,10,13))))
bysort h`wv'coupid: replace chout = chout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace chout = chout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen crin = .
missing_common sp018_ sp019d19sp sp019d20sp sp019d21sp sp019d34sp hhsize ph048dno nchild, result(crin) wave(`wv')
replace crin = .k if nchild == 0
replace crin = 0 if hhsize == 1 | ph048dno == 1 | (nchild != 0 & (sp018_ == 5 | (sp018_ == 1 & ((sp019d19 != 1 | sp019d20 != 1 | sp019d21 != 1 | ///
		sp019d34sp != 1) | ((sp019d1sn != 1 | sp019d2sn != 1 | sp019d3sn != 1 | sp019d4sn != 1 |sp019d5sn != 1 | sp019d6sn != 1 | sp019d7sn != 1) & ///
		(inrange(sn005_1,1,9) | inrange(sn005_1,14,96) | ///
		inrange(sn005_2,1,9) | inrange(sn005_2,14,96) | inrange(sn005_3,1,9) | inrange(sn005_3,14,96) | inrange(sn005_4,1,9) | inrange(sn005_4,14,96) | ///
		inrange(sn005_5,1,9) | inrange(sn005_5,14,96) | inrange(sn005_6,1,9) | inrange(sn005_6,14,96) | inrange(sn005_7,1,9) | inrange(sn005_7,14,96)))))))
replace crin = 1 if nchild != 0 & sp018_ == 1 & ((sp019d19 != 1 | sp019d20 != 1 | sp019d21 != 1 | ///
		sp019d34sp != 1) | ((sp019d1sn != 1 | sp019d2sn != 1 | sp019d3sn != 1 | sp019d4sn != 1 |sp019d5sn != 1 | sp019d6sn != 1 | sp019d7sn != 1) & ///
		(inrange(sn005_1,10,13) | inrange(sn005_2,10,13) | ///
		inrange(sn005_3,10,13) | inrange(sn005_4,10,13) | inrange(sn005_5,10,13) | inrange(sn005_6,10,13) | inrange(sn005_7,10,13))))

gen cspin = .
spouse crin, result(cspin) wave(`wv')

gen h`wv'gccare = .
replace h`wv'gccare = .b if mi(cspin) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .m if (chout == .m | crin == .m | cspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .m if (chout == .m | crin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .r if (chout == .r | crin == .r | cspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .r if (chout == .r | crin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .d if (chout == .d | crin == .d | cspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .d if (chout == .d | crin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .k if (chout == .k | crin == .k | cspin == .k) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .k if (chout == .k | crin == .k) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .x if (chout == .x | crin == .x | cspin == .x) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .x if (chout == .x | crin == .x) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = 0 if (chout == 0 & crin == 0 & cspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = 0 if (chout == 0 & crin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = 1 if (chout == 1 | crin == 1 | cspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = 1 if (chout == 1 | crin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
label variable h`wv'gccare "h`wv'gccare:w`wv' gave informal care to children"
label values h`wv'gccare careg

drop chout crin cspin

***Gave Informal Care to Relatives***
*wave 4 respondent gave informal care to relatives
gen rhout = .
missing_common sp008_ sp009_1sp sp009_2sp sp009_3sp, result(rhout) wave(`wv')
replace rhout = .x if sp009_1sp==96 | sp009_2sp==96 | sp009_3sp==96
replace rhout = 0 if sp008_ == 5 | (sp008_ == 1 & ((inrange(sp009_1sp,19,21) | inrange(sp009_1sp,29,96) | inrange(sp009_2sp,19,21) | ///
		inrange(sp009_2sp,29,96) | inrange(sp009_3sp,19,21) | inrange(sp009_3sp,29,96)) | ((inrange(sp009_1sp,101,107) | inrange(sp009_2sp,101,107) | ///
		inrange(sp009_3sp,101,107)) & (inrange(sn005_1,10,13) | inrange(sn005_1,21,96) | inrange(sn005_2,10,13) | inrange(sn005_2,21,96) | ///
		inrange(sn005_3,10,13) | inrange(sn005_3,21,96) | inrange(sn005_4,10,13) | inrange(sn005_4,21,96) | inrange(sn005_5,10,13) | inrange(sn005_5,21,96) | ///
		inrange(sn005_6,10,13) | inrange(sn005_6,21,96) | inrange(sn005_7,10,13) | inrange(sn005_7,21,96)))))
replace rhout = 1 if sp008_ == 1 & ((inrange(sp009_1sp,1,9) | inrange(sp009_1sp,22,28) | inrange(sp009_2sp,1,9) | inrange(sp009_2sp,22,28) | ///
		inrange(sp009_3sp,1,9) | inrange(sp009_3sp,22,28)) | ((inrange(sp009_1sp,101,107) | inrange(sp009_2sp,101,107) | inrange(sp009_3sp,101,107)) & ///
		(inrange(sn005_1,1,9) | inrange(sn005_1,14,20) | inrange(sn005_2,1,9) | inrange(sn005_2,14,20) | inrange(sn005_3,1,9) | inrange(sn005_3,14,20) | ///
		inrange(sn005_4,1,9) | inrange(sn005_4,14,20) | inrange(sn005_5,1,9) | inrange(sn005_5,14,20) | inrange(sn005_6,1,9) | inrange(sn005_6,14,20) | ///
		inrange(sn005_7,1,9) | inrange(sn005_7,14,20))))
bysort h`wv'coupid: replace rhout = rhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace rhout = rhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rrin = .
missing_common sp018_ sp019d1sp sp019d2sp sp019d3sp sp019d4sp sp019d5sp sp019d6sp sp019d7sp sp019d8sp sp019d9sp ///
		sp019d23sp sp019d24sp sp019d25sp sp019d26sp sp019d27sp sp019d28sp hhsize ph048dno, result(rrin) wave(`wv')
replace rrin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & ((sp019d1sp != 1 | sp019d2sp != 1 | sp019d3sp != 1 | ///
		sp019d4sp != 1 | sp019d5sp != 1 | sp019d6sp != 1 | sp019d7sp != 1 | sp019d8sp != 1 | sp019d9sp != 1 | sp019d23sp != 1 | sp019d24sp != 1 | ///
	sp019d25sp != 1 | sp019d26sp != 1 | sp019d27sp != 1 | sp019d28sp != 1) | ((sp019d1sn != 1 | sp019d2sn != 1 | sp019d3sn != 1 | sp019d4sn != 1 | ///
	sp019d5sn != 1 | sp019d6sn != 1 | sp019d7sn != 1) & (inrange(sn005_1,10,13) | inrange(sn005_1,21,96) | inrange(sn005_2,10,13) | inrange(sn005_2,21,96) | ///
		inrange(sn005_3,10,13) | inrange(sn005_3,21,96) | inrange(sn005_4,10,13) | inrange(sn005_4,21,96) | inrange(sn005_5,10,13) | inrange(sn005_5,21,96) | ///
		inrange(sn005_6,10,13) | inrange(sn005_6,21,96) | inrange(sn005_7,10,13) | inrange(sn005_7,21,96))))))
replace rrin = 1 if sp018_ == 1 & ((sp019d1sp == 1 | sp019d2sp == 1 | sp019d3sp == 1 | sp019d4sp == 1 | sp019d5sp == 1 | sp019d6sp == 1 | sp019d7sp == 1 | ///
		sp019d8sp == 1 | sp019d9sp == 1 | sp019d23sp == 1 | sp019d24sp == 1 | sp019d25sp == 1 | sp019d26sp == 1 | sp019d27sp == 1 | sp019d28sp == 1) | ///
		((sp019d1sn == 1 | sp019d2sn == 1 | sp019d3sn == 1 | sp019d4sn == 1 | ///
	sp019d5sn == 1 | sp019d6sn == 1 | sp019d7sn == 1) &  (inrange(sn005_1,1,9) | inrange(sn005_1,14,20) | inrange(sn005_2,1,9) | inrange(sn005_2,14,20) | inrange(sn005_3,1,9) | inrange(sn005_3,14,20) | ///
		inrange(sn005_4,1,9) | inrange(sn005_4,14,20) | inrange(sn005_5,1,9) | inrange(sn005_5,14,20) | inrange(sn005_6,1,9) | inrange(sn005_6,14,20) | ///
		inrange(sn005_7,1,9) | inrange(sn005_7,14,20))))

gen rspin = .
spouse rrin, result(rspin) wave(`wv')

gen h`wv'grcare = .
replace h`wv'grcare = .b if mi(rspin) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .m if (rhout == .m | rrin == .m | rspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .m if (rhout == .m | rrin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = .r if (rhout == .r | rrin == .r | rspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .r if (rhout == .r | rrin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = .d if (rhout == .d | rrin == .d | rspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .d if (rhout == .d | rrin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = .x if (rhout == .x | rrin == .x | rspin == .x) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .x if (rhout == .x | rrin == .x) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = 0 if (rhout == 0 & rrin == 0 & rspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = 0 if (rhout == 0 & rrin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = 1 if (rhout == 1 | rrin == 1 | rspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = 1 if (rhout == 1 | rrin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
label variable h`wv'grcare "h`wv'grcare:w`wv' gave informal care to relatives"
label values h`wv'grcare careg

drop rhout rrin rspin

***Gave Informal Care to non-relatives***
*wave 4 respondent gave informal care to non-relatives
gen fhout = .
missing_common sp008_ sp009_1sp sp009_2sp sp009_3sp, result(fhout) wave(`wv')
replace fhout = .x if sp009_1sp==96 | sp009_2sp==96 | sp009_3sp==96
replace fhout = 0 if sp008_ == 5 | (sp008_ == 1 & ((inrange(sp009_1sp,1,28) | inlist(sp009_1sp,34,96) | inrange(sp009_2sp,1,28) | inlist(sp009_2sp,34,96) | ///
		inrange(sp009_3sp,1,28) | inlist(sp009_3sp,34,96) | ((inrange(sp009_1sp,101,107) | inrange(sp009_2sp,101,107) | inrange(sp009_3sp,101,107)) & ///
		(!inrange(sn005_1,21,27) | !inrange(sn005_2,21,27) | !inrange(sn005_3,21,27) | !inrange(sn005_4,21,27) | !inrange(sn005_5,21,27) | !inrange(sn005_6,21,27) | ///
		!inrange(sn005_7,21,27))))))
replace fhout = 1 if sp008_ == 1 & ((inrange(sp009_1sp,29,33) | inrange(sp009_1sp,35,37) | inrange(sp009_2sp,29,33) | inrange(sp009_2sp,35,37) | ///
		inrange(sp009_3sp,29,33) | inrange(sp009_3sp,35,37)) | ((inrange(sp009_1sp,101,107) | inrange(sp009_2sp,101,107) | inrange(sp009_3sp,101,107)) & ///
		(inrange(sn005_1,21,27) | inrange(sn005_2,21,27) | inrange(sn005_3,21,27) | inrange(sn005_4,21,27) | inrange(sn005_5,21,27) | inrange(sn005_6,21,27) | ///
		inrange(sn005_7,21,27))))
bysort h`wv'coupid: replace fhout = fhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace fhout = fhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen frin = .
missing_common sp018_ sp019d29sp sp019d30sp sp019d31sp sp019d32sp sp019d35sp sp019d36sp sp019d37sp hhsize ph048dno, result(frin) wave(`wv')
replace frin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & ((sp019d29sp != 1 | sp019d30sp != 1 | sp019d31sp != 1 | ///
		sp019d32sp != 1 | sp019d35sp != 1 | sp019d36sp != 1 | sp019d37sp != 1) | ((sp019d1sn != 1 | sp019d2sn != 1 | sp019d3sn != 1 | sp019d4sn != 1 | ///
		sp019d5sn != 1 | sp019d6sn != 1 | sp019d7sn != 1) &  (!inrange(sn005_1,21,27) | !inrange(sn005_2,21,27) | !inrange(sn005_3,21,27) | ///
		!inrange(sn005_4,21,27) | !inrange(sn005_5,21,27) | !inrange(sn005_6,21,27) | !inrange(sn005_7,21,27))))))
replace frin = 1 if sp018_ == 1 & ((sp019d29sp == 1 | sp019d30sp == 1 | sp019d31sp == 1 | sp019d32sp == 1 | sp019d35sp == 1 | sp019d36sp == 1 | ///
		sp019d37sp == 1) | ((sp019d1sn != 1 | sp019d2sn != 1 | sp019d3sn != 1 | sp019d4sn != 1 | ///
		sp019d5sn != 1 | sp019d6sn != 1 | sp019d7sn != 1) & (inrange(sn005_1,21,27) | inrange(sn005_2,21,27) | inrange(sn005_3,21,27) | inrange(sn005_4,21,27) | inrange(sn005_5,21,27) | inrange(sn005_6,21,27) | ///
		inrange(sn005_7,21,27))))

gen fspin = .
spouse frin, result(fspin) wave(`wv')

gen h`wv'gfcare = .
replace h`wv'gfcare = .b if mi(fspin) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .m if (fhout == .m | frin == .m | fspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .m if (fhout == .m | frin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = .r if (fhout == .r | frin == .r | fspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .r if (fhout == .r | frin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = .d if (fhout == .d | frin == .d | fspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .d if (fhout == .d | frin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = .x if (fhout == .x | frin == .x | fspin == .x) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .x if (fhout == .x | frin == .x) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = 0 if (fhout == 0 & frin == 0 & fspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = 0 if (fhout == 0 & frin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = 1 if (fhout == 1 | frin == 1 | fspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = 1 if (fhout == 1 | frin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
label variable h`wv'gfcare "h`wv'gfcare:w`wv' gave informal care to non-relatives"
label values h`wv'gfcare careg

drop fhout frin fspin

***Gave Informal Care to Grandchildren***
*wave 4 respondent gave care to grandchildren
gen khout = .
missing_common sp014_ ngrchild, result(khout) wave(`wv')
replace khout = .g if ngrchild == 0
replace khout = 0 if ngrchild != 0 & sp014_ == 5
replace khout = 1 if ngrchild != 0 & sp014_ == 1
bysort h`wv'coupid: replace khout = khout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace khout = khout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen krin = .
missing_common sp018_ sp019d22sp hhsize ph048dno, result(krin) wave(`wv')
replace krin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & (sp019d22sp != 1 | ///
		((sp019d1sn != 1 | sp019d2sn != 1 | sp019d3sn != 1 | sp019d4sn != 1 | sp019d5sn != 1 | sp019d6sn != 1 | ///
		sp019d7sn != 1) & (sn005_1 != 14 | sn005_2 != 14 | sn005_3 != 14 | sn005_4 != 14 | sn005_5 != 14 | ///
		sn005_6 != 14 | sn005_7 != 14)))))
replace krin = 1 if sp018_ == 1 & (sp019d22 == 1 | ((sp019d1sn == 1 | sp019d2sn == 1 | sp019d3sn == 1 | ///
		sp019d4sn == 1 | sp019d5sn == 1 | sp019d6sn == 1 | ///
		sp019d7sn == 1) & (sn005_1 == 14 | sn005_2 == 14 | sn005_3 == 14 | sn005_4 == 14 | sn005_5 == 14 | ///
		sn005_6 == 14 | sn005_7 == 14)))

gen kspin = .
spouse krin, result(kspin) wave(`wv')

gen h`wv'gkcare = .
replace h`wv'gkcare = .b if mi(kspin) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .m if (khout == .m | krin == .m | kspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .m if (khout == .m | krin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .r if (khout == .r | krin == .r | kspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .r if (khout == .r | krin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .d if (khout == .d | krin == .d | kspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .d if (khout == .d | krin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .g if (khout == .g | krin == .g | kspin == .g) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .g if (khout == .g | krin == .g) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = 0 if (khout == 0 & krin == 0 & kspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = 0 if (khout == 0 & krin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = 1 if (khout == 1 | krin == 1 | kspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = 1 if (khout == 1 | krin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
label variable h`wv'gkcare "h`wv'gkcare:w`wv' gave care to grandchildren"
label values h`wv'gkcare careg

drop khout krin kspin

******Transfer Amounts Not Asked Starting in Wave 4***

***Any Transfer to Children/Grandchildren***
*wave 4 household any transfer to children/grandchildren
gen h`wv'tcany = .
missing_common ft002_ ft003_1ft ft003_2ft ft003_3ft sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7, result(h`wv'tcany) wave(`wv')
replace h`wv'tcany = 0 if ft002_ == 5 | (ft002_ == 1 & ((inrange(ft003_1ft,1,18) | inrange(ft003_1ft,23,33) | inrange(ft003_1ft,35,96) | ///
		inrange(ft003_2ft,1,18) | inrange(ft003_2ft,23,33) | inrange(ft003_2ft,35,96) | inrange(ft003_3ft,1,18) | ///
		inrange(ft003_3ft,23,33) | inrange(ft003_3ft,35,96)) | ///
		((ft003_1ft == 101 | ft003_2ft == 101 | ft003_3ft == 101) & (inrange(sn005_1,1,9) | inrange(sn005_1,15,96))) | ///
		((ft003_1ft == 102 | ft003_2ft == 102 | ft003_3ft == 102) & (inrange(sn005_2,1,9) | inrange(sn005_2,15,96))) | ///
		((ft003_1ft == 103 | ft003_2ft == 103 | ft003_3ft == 103) & (inrange(sn005_3,1,9) | inrange(sn005_3,15,96))) | ///
		((ft003_1ft == 104 | ft003_2ft == 104 | ft003_3ft == 104) & (inrange(sn005_4,1,9) | inrange(sn005_4,15,96))) | ///
		((ft003_1ft == 105 | ft003_2ft == 105 | ft003_3ft == 105) & (inrange(sn005_5,1,9) | inrange(sn005_5,15,96))) | ///
		((ft003_1ft == 106 | ft003_2ft == 106 | ft003_3ft == 106) & (inrange(sn005_6,1,9) | inrange(sn005_6,15,96))) | ///
		((ft003_1ft == 107 | ft003_2ft == 107 | ft003_3ft == 107) & (inrange(sn005_7,1,9) | inrange(sn005_7,15,96)))))
replace h`wv'tcany = 1 if ft002_ == 1 & ((inrange(ft003_1ft,19,22) | ft003_1ft == 34 | ///
        inrange(ft003_2ft,19,22) | ft003_2ft == 34 | ///
		inrange(ft003_3ft,19,22) | ft003_3ft == 34) | ///
		((ft003_1ft == 101 | ft003_2ft == 101 | ft003_3ft == 101) & inrange(sn005_1,10,14)) | ///
		((ft003_1ft == 102 | ft003_2ft == 102 | ft003_3ft == 102) & inrange(sn005_2,10,14)) | ///
		((ft003_1ft == 103 | ft003_2ft == 103 | ft003_3ft == 103) & inrange(sn005_3,10,14)) | ///
		((ft003_1ft == 104 | ft003_2ft == 104 | ft003_3ft == 104) & inrange(sn005_4,10,14)) | ///
		((ft003_1ft == 105 | ft003_2ft == 105 | ft003_3ft == 105) & inrange(sn005_5,10,14)) | ///
		((ft003_1ft == 106 | ft003_2ft == 106 | ft003_3ft == 106) & inrange(sn005_6,10,14)) | ///
		((ft003_1ft == 107 | ft003_2ft == 107 | ft003_3ft == 107) & inrange(sn005_7,10,14)))
label variable h`wv'tcany "h`wv'tcany:w`wv' any transfer to children/grandchildren"
label values h`wv'tcany trans

bysort h`wv'coupid: replace h`wv'tcany = h`wv'tcany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tcany = h`wv'tcany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers to Parents***
*wave 4 household any transfers to parents
gen h`wv'tpany = .
missing_common ft002_ ft003_1ft ft003_2ft ft003_3ft sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7, result(h`wv'tpany) wave(`wv')
replace h`wv'tpany = 0 if ft002_ == 5 | (ft002_ == 1 & ((ft003_1ft == 1 | inrange(ft003_1ft,8,96) | ///
		ft003_2ft == 1 | inrange(ft003_2ft,8,96) | ///
		ft003_3ft == 1 | inrange(ft003_3ft,8,96)) | ///
		((ft003_1ft == 101 | ft003_2ft == 101 | ft003_3ft == 101) & (sn005_1 == 1 | inrange(sn005_1,8,96))) | ///
		((ft003_1ft == 102 | ft003_2ft == 102 | ft003_3ft == 102) & (sn005_2 == 1 | inrange(sn005_2,8,96))) | ///
		((ft003_1ft == 103 | ft003_2ft == 103 | ft003_3ft == 103) & (sn005_3 == 1 | inrange(sn005_3,8,96))) | ///
		((ft003_1ft == 104 | ft003_2ft == 104 | ft003_3ft == 104) & (sn005_4 == 1 | inrange(sn005_4,8,96))) | ///
		((ft003_1ft == 105 | ft003_2ft == 105 | ft003_3ft == 105) & (sn005_5 == 1 | inrange(sn005_5,8,96))) | ///
		((ft003_1ft == 106 | ft003_2ft == 106 | ft003_3ft == 106) & (sn005_6 == 1 | inrange(sn005_6,8,96))) | ///
		((ft003_1ft == 107 | ft003_2ft == 107 | ft003_3ft == 107) & (sn005_7 == 1 | inrange(sn005_7,8,96)))))
replace h`wv'tpany = 1 if ft002_ == 1 & ((inrange(ft003_1ft,2,7) | inrange(ft003_2ft,2,7) | inrange(ft003_3ft,2,7)) | ///
        ((ft003_1ft == 101 | ft003_2ft == 101 | ft003_3ft == 101) & inrange(sn005_1,2,7)) | ///
		((ft003_1ft == 102 | ft003_2ft == 102 | ft003_3ft == 102) & inrange(sn005_2,2,7)) | ///
		((ft003_1ft == 103 | ft003_2ft == 103 | ft003_3ft == 103) & inrange(sn005_3,2,7)) | ///
		((ft003_1ft == 104 | ft003_2ft == 104 | ft003_3ft == 104) & inrange(sn005_4,2,7)) | ///
		((ft003_1ft == 105 | ft003_2ft == 105 | ft003_3ft == 105) & inrange(sn005_5,2,7)) | ///
		((ft003_1ft == 106 | ft003_2ft == 106 | ft003_3ft == 106) & inrange(sn005_6,2,7)) | ///
		((ft003_1ft == 107 | ft003_2ft == 107 | ft003_3ft == 107) & inrange(sn005_7,2,7)))
label variable h`wv'tpany "h`wv'tpany:w`wv' any transfer to parents"
label values h`wv'tpany trans

bysort h`wv'coupid: replace h`wv'tpany = h`wv'tpany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tpany = h`wv'tpany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers to Other Relatives***
*wave 4 household any transfers to other relatives
gen h`wv'toany = .
missing_common ft002_ ft003_1ft ft003_2ft ft003_3ft sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7, result(h`wv'toany) wave(`wv')
replace h`wv'toany = 0 if ft002_ == 5 | (ft002_ == 1 & ((inrange(ft003_1ft,2,7) | inrange(ft003_1ft,10,22) | inrange(ft003_1ft,29,96) | /// 
		inrange(ft003_2ft,2,7) | inrange(ft003_2ft,10,22) | inrange(ft003_2ft,29,96) | ///
		inrange(ft003_3ft,2,7) | inrange(ft003_3ft,10,22) | inrange(ft003_3ft,29,96)) | ///
		((ft003_1ft == 101 | ft003_2ft == 101 | ft003_3ft == 101) & (inrange(sn005_1,2,7) | inrange(sn005_1,10,14) | inrange(sn005_1,21,96))) | ///
		((ft003_1ft == 102 | ft003_2ft == 102 | ft003_3ft == 102) & (inrange(sn005_2,2,7) | inrange(sn005_2,10,14) | inrange(sn005_2,21,96))) | ///
		((ft003_1ft == 103 | ft003_2ft == 103 | ft003_3ft == 103) & (inrange(sn005_3,2,7) | inrange(sn005_3,10,14) | inrange(sn005_3,21,96))) | ///
		((ft003_1ft == 104 | ft003_2ft == 104 | ft003_3ft == 104) & (inrange(sn005_4,2,7) | inrange(sn005_4,10,14) | inrange(sn005_4,21,96))) | ///
		((ft003_1ft == 105 | ft003_2ft == 105 | ft003_3ft == 105) & (inrange(sn005_5,2,7) | inrange(sn005_5,10,14) | inrange(sn005_5,21,96))) | ///
		((ft003_1ft == 106 | ft003_2ft == 106 | ft003_3ft == 106) & (inrange(sn005_6,2,7) | inrange(sn005_6,10,14) | inrange(sn005_6,21,96))) | ///
		((ft003_1ft == 107 | ft003_2ft == 107 | ft003_3ft == 107) & (inrange(sn005_7,2,7) | inrange(sn005_7,10,14) | inrange(sn005_7,21,96)))))
replace h`wv'toany = 1 if ft002_ == 1 & ((inlist(ft003_1ft,1,8,9) | inrange(ft003_1ft,23,28) | ///
        inlist(ft003_2ft,1,8,9) | inrange(ft003_2ft,23,28) | ///
		inlist(ft003_3ft,1,8,9) | inrange(ft003_3ft,23,28)) | ///
		((ft003_1ft == 101 | ft003_2ft == 101 | ft003_3ft == 101) & (inlist(sn005_1,1,8,9) | inrange(sn005_1,15,20))) | ///
		((ft003_1ft == 102 | ft003_2ft == 102 | ft003_3ft == 102) & (inlist(sn005_2,1,8,9) | inrange(sn005_2,15,20))) | ///
		((ft003_1ft == 103 | ft003_2ft == 103 | ft003_3ft == 103) & (inlist(sn005_3,1,8,9) | inrange(sn005_3,15,20))) | ///
		((ft003_1ft == 104 | ft003_2ft == 104 | ft003_3ft == 104) & (inlist(sn005_4,1,8,9) | inrange(sn005_4,15,20))) | ///
		((ft003_1ft == 105 | ft003_2ft == 105 | ft003_3ft == 105) & (inlist(sn005_5,1,8,9) | inrange(sn005_5,15,20))) | ///
		((ft003_1ft == 106 | ft003_2ft == 106 | ft003_3ft == 106) & (inlist(sn005_6,1,8,9) | inrange(sn005_6,15,20))) | ///
		((ft003_1ft == 107 | ft003_2ft == 107 | ft003_3ft == 107) & (inlist(sn005_7,1,8,9) | inrange(sn005_7,15,20))))
label variable h`wv'toany "h`wv'toany:w`wv' any transfer to other relatives"
label values h`wv'toany trans

bysort h`wv'coupid: replace h`wv'toany = h`wv'toany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'toany = h`wv'toany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers to Other Non-Relatives***
*wave 4 household any transfers to other non-relatives
gen h`wv'tfany = .
missing_common ft002_ ft003_1ft ft003_2ft ft003_3ft sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7, result(h`wv'tfany) wave(`wv')
replace h`wv'tfany = 0 if ft002_ == 5 | (ft002_ == 1 & ((inrange(ft003_1ft,1,28) | inlist(ft003_1ft,34,96) | ///
        inrange(ft003_2ft,1,28) | inlist(ft003_2ft,34,96) | ///
		inrange(ft003_3ft,1,28) | inlist(ft003_3ft,34,96)) | ///
		((ft003_1ft == 101 | ft003_2ft == 101 | ft003_3ft == 101) & (inrange(sn005_1,2,7) | inrange(sn005_1,1,20) | inrange(sn005_1,28,96))) | ///
		((ft003_1ft == 102 | ft003_2ft == 102 | ft003_3ft == 102) & (inrange(sn005_2,2,7) | inrange(sn005_2,1,20) | inrange(sn005_2,28,96))) | ///
		((ft003_1ft == 103 | ft003_2ft == 103 | ft003_3ft == 103) & (inrange(sn005_3,2,7) | inrange(sn005_3,1,20) | inrange(sn005_3,28,96))) | ///
		((ft003_1ft == 104 | ft003_2ft == 104 | ft003_3ft == 104) & (inrange(sn005_4,2,7) | inrange(sn005_4,1,20) | inrange(sn005_4,28,96))) | ///
		((ft003_1ft == 105 | ft003_2ft == 105 | ft003_3ft == 105) & (inrange(sn005_5,2,7) | inrange(sn005_5,1,20) | inrange(sn005_5,28,96))) | ///
		((ft003_1ft == 106 | ft003_2ft == 106 | ft003_3ft == 106) & (inrange(sn005_6,2,7) | inrange(sn005_6,1,20) | inrange(sn005_6,28,96))) | ///
		((ft003_1ft == 107 | ft003_2ft == 107 | ft003_3ft == 107) & (inrange(sn005_7,2,7) | inrange(sn005_7,1,20) | inrange(sn005_7,28,96)))))
replace h`wv'tfany = 1 if ft002_ == 1 & ((inrange(ft003_1ft,29,33) | inrange(ft003_1ft,35,57) | ///
        inrange(ft003_2ft,29,33) | inrange(ft003_2ft,35,37) | ///
		inrange(ft003_3ft,29,33) | inrange(ft003_3ft,35,37)) | ///
		((ft003_1ft == 101 | ft003_2ft == 101 | ft003_3ft == 101) & inrange(sn005_1,21,27)) | ///
		((ft003_1ft == 102 | ft003_2ft == 102 | ft003_3ft == 102) & inrange(sn005_2,21,27)) | ///
		((ft003_1ft == 103 | ft003_2ft == 103 | ft003_3ft == 103) & inrange(sn005_3,21,27)) | ///
		((ft003_1ft == 104 | ft003_2ft == 104 | ft003_3ft == 104) & inrange(sn005_4,21,27)) | ///
		((ft003_1ft == 105 | ft003_2ft == 105 | ft003_3ft == 105) & inrange(sn005_5,21,27)) | ///
		((ft003_1ft == 106 | ft003_2ft == 106 | ft003_3ft == 106) & inrange(sn005_6,21,27)) | ///
		((ft003_1ft == 107 | ft003_2ft == 107 | ft003_3ft == 107) & inrange(sn005_7,21,27)))
label variable h`wv'tfany "h`wv'tfany:w`wv' any transfer to other non-relatives"
label values h`wv'tfany trans

bysort h`wv'coupid: replace h`wv'tfany = h`wv'tfany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tfany = h`wv'tfany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers from Children/Grandchildren***
*wave 4 household any transfers from children/grandchildren
gen h`wv'fcany = .
missing_common ft009_ ft010_1ft ft010_2ft ft010_3ft sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7, result(h`wv'fcany) wave(`wv')
replace h`wv'fcany = 0 if ft009_ == 5 | (ft009_ == 1 & ((inrange(ft010_1ft,1,18) | inrange(ft010_1ft,23,33) | inrange(ft010_1ft,35,96) | ///
		inrange(ft010_2ft,1,18) | inrange(ft010_2ft,23,33) | inrange(ft010_2ft,35,96) | ///
		inrange(ft010_3ft,1,9) | inrange(ft010_3ft,23,33) | inrange(ft010_3ft,35,96)) | ///
		((ft010_1ft == 101 | ft010_2ft == 101 | ft010_3ft == 101) & (inrange(sn005_1,1,9) | inrange(sn005_1,15,96))) | ///
		((ft010_1ft == 102 | ft010_2ft == 102 | ft010_3ft == 102) & (inrange(sn005_2,1,9) | inrange(sn005_2,15,96))) | ///
		((ft010_1ft == 103 | ft010_2ft == 103 | ft010_3ft == 103) & (inrange(sn005_3,1,9) | inrange(sn005_3,15,96))) | ///
		((ft010_1ft == 104 | ft010_2ft == 104 | ft010_3ft == 104) & (inrange(sn005_4,1,9) | inrange(sn005_4,15,96))) | ///
		((ft010_1ft == 105 | ft010_2ft == 105 | ft010_3ft == 105) & (inrange(sn005_5,1,9) | inrange(sn005_5,15,96))) | ///
		((ft010_1ft == 106 | ft010_2ft == 106 | ft010_3ft == 106) & (inrange(sn005_6,1,9) | inrange(sn005_6,15,96))) | ///
		((ft010_1ft == 107 | ft010_2ft == 107 | ft010_3ft == 107) & (inrange(sn005_7,1,9) | inrange(sn005_7,15,96)))))
replace h`wv'fcany = 1 if ft009_ == 1 & ((inrange(ft010_1ft,19,22) | ft010_1ft == 34 | ///
        inrange(ft010_2ft,19,22) | ft010_2ft == 34 | ///
		inrange(ft010_3ft,19,22) | ft010_3ft == 34) | ///
		((ft010_1ft == 101 | ft010_2ft == 101 | ft010_3ft == 101) & inrange(sn005_1,10,14)) | ///
		((ft010_1ft == 102 | ft010_2ft == 102 | ft010_3ft == 102) & inrange(sn005_2,10,14)) | ///
		((ft010_1ft == 103 | ft010_2ft == 103 | ft010_3ft == 103) & inrange(sn005_3,10,14)) | ///
		((ft010_1ft == 104 | ft010_2ft == 104 | ft010_3ft == 104) & inrange(sn005_4,10,14)) | ///
		((ft010_1ft == 105 | ft010_2ft == 105 | ft010_3ft == 105) & inrange(sn005_5,10,14)) | ///
		((ft010_1ft == 106 | ft010_2ft == 106 | ft010_3ft == 106) & inrange(sn005_6,10,14)) | ///
		((ft010_1ft == 107 | ft010_2ft == 107 | ft010_3ft == 107) & inrange(sn005_7,10,14)))
label variable h`wv'fcany "h`wv'fcany:w`wv' any transfers from children/grandchildren"
label values h`wv'fcany trans

bysort h`wv'coupid: replace h`wv'fcany = h`wv'fcany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fcany = h`wv'fcany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers from Parents***
*wave 4 household any transfers from parents
gen h`wv'fpany = .
missing_common ft009_ ft010_1ft ft010_2ft ft010_3ft sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7, result(h`wv'fpany) wave(`wv')
replace h`wv'fpany = 0 if ft009_ == 5 | (ft009_ == 1 & ((ft010_1ft == 1 | inrange(ft010_1ft,8,96) | ///
		ft010_2ft == 1 | inrange(ft010_2ft,8,96) | ///
		ft010_3ft == 1 | inrange(ft010_3ft,8,96)) | ///
		((ft010_1ft == 101 | ft010_2ft == 101 | ft010_3ft == 101) & (sn005_1 == 1 | inrange(sn005_1,8,96))) | ///
		((ft010_1ft == 102 | ft010_2ft == 102 | ft010_3ft == 102) & (sn005_2 == 1 | inrange(sn005_2,8,96))) | ///
		((ft010_1ft == 103 | ft010_2ft == 103 | ft010_3ft == 103) & (sn005_3 == 1 | inrange(sn005_3,8,96))) | ///
		((ft010_1ft == 104 | ft010_2ft == 104 | ft010_3ft == 104) & (sn005_4 == 1 | inrange(sn005_4,8,96))) | ///
		((ft010_1ft == 105 | ft010_2ft == 105 | ft010_3ft == 105) & (sn005_5 == 1 | inrange(sn005_5,8,96))) | ///
		((ft010_1ft == 106 | ft010_2ft == 106 | ft010_3ft == 106) & (sn005_6 == 1 | inrange(sn005_6,8,96))) | ///
		((ft010_1ft == 107 | ft010_2ft == 107 | ft010_3ft == 107) & (sn005_7 == 1 | inrange(sn005_7,8,96)))))
replace h`wv'fpany = 1 if ft009_ == 1 & ((inrange(ft010_1ft,2,7) | inrange(ft010_2ft,2,7) | inrange(ft010_3ft,2,7)) | ///
		((ft010_1ft == 101 | ft010_2ft == 101 | ft010_3ft == 101) & inrange(sn005_1,2,7)) | ///
		((ft010_1ft == 102 | ft010_2ft == 102 | ft010_3ft == 102) & inrange(sn005_2,2,7)) | ///
		((ft010_1ft == 103 | ft010_2ft == 103 | ft010_3ft == 103) & inrange(sn005_3,2,7)) | ///
		((ft010_1ft == 104 | ft010_2ft == 104 | ft010_3ft == 104) & inrange(sn005_4,2,7)) | ///
		((ft010_1ft == 105 | ft010_2ft == 105 | ft010_3ft == 105) & inrange(sn005_5,2,7)) | ///
		((ft010_1ft == 106 | ft010_2ft == 106 | ft010_3ft == 106) & inrange(sn005_6,2,7)) | ///
		((ft010_1ft == 107 | ft010_2ft == 107 | ft010_3ft == 107) & inrange(sn005_7,2,7)))
label variable h`wv'fpany "h`wv'fpany:w`wv' any transfers from parents"
label values h`wv'fpany trans

bysort h`wv'coupid: replace h`wv'fpany = h`wv'fpany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fpany = h`wv'fpany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers from Other Relatives***
*wave 4 household any transfers from other relatives
gen h`wv'foany = .
missing_common ft009_ ft010_1ft ft010_2ft ft010_3ft sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7, result(h`wv'foany) wave(`wv')
replace h`wv'foany = 0 if ft009_ == 5 | (ft009_ == 1 & ((inrange(ft010_1ft,2,7) | inrange(ft010_1ft,10,22) | inrange(ft010_1ft,29,96) | ///
        inrange(ft010_2ft,2,7) | inrange(ft010_2ft,10,22) | inrange(ft010_2ft,29,96) | ///
        inrange(ft010_3ft,2,7) | inrange(ft010_3ft,10,22) | inrange(ft010_3ft,29,96)) | ///
        ((ft010_1ft == 101 | ft010_2ft == 101 | ft010_3ft == 101) & (inrange(sn005_1,2,7) | inrange(sn005_1,10,14) | inrange(sn005_1,21,96))) | ///
		((ft010_1ft == 102 | ft010_2ft == 102 | ft010_3ft == 102) & (inrange(sn005_2,2,7) | inrange(sn005_2,10,14) | inrange(sn005_2,21,96))) | ///
		((ft010_1ft == 103 | ft010_2ft == 103 | ft010_3ft == 103) & (inrange(sn005_3,2,7) | inrange(sn005_3,10,14) | inrange(sn005_3,21,96))) | ///
		((ft010_1ft == 104 | ft010_2ft == 104 | ft010_3ft == 104) & (inrange(sn005_4,2,7) | inrange(sn005_4,10,14) | inrange(sn005_4,21,96))) | ///
		((ft010_1ft == 105 | ft010_2ft == 105 | ft010_3ft == 105) & (inrange(sn005_5,2,7) | inrange(sn005_5,10,14) | inrange(sn005_5,21,96))) | ///
		((ft010_1ft == 106 | ft010_2ft == 106 | ft010_3ft == 106) & (inrange(sn005_6,2,7) | inrange(sn005_6,10,14) | inrange(sn005_6,21,96))) | ///
		((ft010_1ft == 107 | ft010_2ft == 107 | ft010_3ft == 107) & (inrange(sn005_7,2,7) | inrange(sn005_7,10,14) | inrange(sn005_7,21,96)))))
replace h`wv'foany = 1 if ft009_ == 1 & ((inlist(ft010_1ft,1,8,9) | inrange(ft010_1ft,23,28) | ///
        inlist(ft010_2ft,1,8,9) | inrange(ft010_2ft,23,28) | ///
		inlist(ft010_3ft,1,8,9) | inrange(ft010_3ft,23,28)) | ///
		((ft010_1ft == 101 | ft010_2ft == 101 | ft010_3ft == 101) & (inlist(sn005_1,1,8,9) | inrange(sn005_1,15,20))) | ///
		((ft010_1ft == 102 | ft010_2ft == 102 | ft010_3ft == 102) & (inlist(sn005_2,1,8,9) | inrange(sn005_2,15,20))) | ///
		((ft010_1ft == 103 | ft010_2ft == 103 | ft010_3ft == 103) & (inlist(sn005_3,1,8,9) | inrange(sn005_3,15,20))) | ///
		((ft010_1ft == 104 | ft010_2ft == 104 | ft010_3ft == 104) & (inlist(sn005_4,1,8,9) | inrange(sn005_4,15,20))) | ///
		((ft010_1ft == 105 | ft010_2ft == 105 | ft010_3ft == 105) & (inlist(sn005_5,1,8,9) | inrange(sn005_5,15,20))) | ///
		((ft010_1ft == 106 | ft010_2ft == 106 | ft010_3ft == 106) & (inlist(sn005_6,1,8,9) | inrange(sn005_6,15,20))) | ///
		((ft010_1ft == 107 | ft010_2ft == 107 | ft010_3ft == 107) & (inlist(sn005_7,1,8,9) | inrange(sn005_7,15,20))))
label variable h`wv'foany "h`wv'foany:w`wv' any transfers from other relatives"
label values h`wv'foany trans

bysort h`wv'coupid: replace h`wv'foany = h`wv'foany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'foany = h`wv'foany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers from Other Non-Relatives***
*wave 4 household any transfers from other non-relatives
gen h`wv'ffany = .
missing_common ft009_ ft010_1ft ft010_2ft ft010_3ft sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7, result(h`wv'ffany) wave(`wv')
replace h`wv'ffany = .x if ft010_1ft == 96 | ft010_2ft == 96 | ft010_3ft == 96 | sn005_1 == 96 | sn005_2 == 96 | sn005_3 == 96 | sn005_4 == 96 | ///
		sn005_5 == 96 | sn005_6 == 96 | sn005_7 == 96
replace h`wv'ffany = 0 if ft009_ == 5 | (ft009_ == 1 & ((inrange(ft010_1ft,1,28) | ft010_1ft == 34 | ///
        inrange(ft010_2ft,1,28) | ft010_2ft == 34 | ///
        inrange(ft010_3ft,1,28) | ft010_3ft == 34) | ///
        ((ft010_1ft == 101 | ft010_2ft == 101 | ft010_3ft == 101) & (inrange(sn005_1,2,7) | inrange(sn005_1,1,20) | inrange(sn005_1,28,96))) | ///
		((ft010_1ft == 102 | ft010_2ft == 102 | ft010_3ft == 102) & (inrange(sn005_2,2,7) | inrange(sn005_2,1,20) | inrange(sn005_2,28,96))) | ///
		((ft010_1ft == 103 | ft010_2ft == 103 | ft010_3ft == 103) & (inrange(sn005_3,2,7) | inrange(sn005_3,1,20) | inrange(sn005_3,28,96))) | ///
		((ft010_1ft == 104 | ft010_2ft == 104 | ft010_3ft == 104) & (inrange(sn005_4,2,7) | inrange(sn005_4,1,20) | inrange(sn005_4,28,96))) | ///
		((ft010_1ft == 105 | ft010_2ft == 105 | ft010_3ft == 105) & (inrange(sn005_5,2,7) | inrange(sn005_5,1,20) | inrange(sn005_5,28,96))) | ///
		((ft010_1ft == 106 | ft010_2ft == 106 | ft010_3ft == 106) & (inrange(sn005_6,2,7) | inrange(sn005_6,1,20) | inrange(sn005_6,28,96))) | ///
		((ft010_1ft == 107 | ft010_2ft == 107 | ft010_3ft == 107) & (inrange(sn005_7,2,7) | inrange(sn005_7,1,20) | inrange(sn005_7,28,96)))))
replace h`wv'ffany = 1 if ft009_ == 1 & ((inrange(ft010_1ft,29,33) | inrange(ft010_1ft,35,37) | ///
        inrange(ft010_2ft,29,33) | inrange(ft010_2ft,35,37) | ///
		inrange(ft010_3ft,29,33) | inrange(ft010_3ft,35,37)) | ///
		((ft010_1ft == 101 | ft010_2ft == 101 | ft010_3ft == 101) & inrange(sn005_1,21,27)) | ///
		((ft010_1ft == 102 | ft010_2ft == 102 | ft010_3ft == 102) & inrange(sn005_2,21,27)) | ///
		((ft010_1ft == 103 | ft010_2ft == 103 | ft010_3ft == 103) & inrange(sn005_3,21,27)) | ///
		((ft010_1ft == 104 | ft010_2ft == 104 | ft010_3ft == 104) & inrange(sn005_4,21,27)) | ///
		((ft010_1ft == 105 | ft010_2ft == 105 | ft010_3ft == 105) & inrange(sn005_5,21,27)) | ///
		((ft010_1ft == 106 | ft010_2ft == 106 | ft010_3ft == 106) & inrange(sn005_6,21,27)) | ///
		((ft010_1ft == 107 | ft010_2ft == 107 | ft010_3ft == 107) & inrange(sn005_7,21,27)))
label variable h`wv'ffany "h`wv'ffany:w`wv' any transfers from other non-relatives"
label values h`wv'ffany trans

bysort h`wv'coupid: replace h`wv'ffany = h`wv'ffany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'ffany = h`wv'ffany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Social Activities in Past Year***changed from previous waves because frequency activities no longer asked
*wave 4 respondent any yearly social activities
gen r`wv'socyr = .
missing_common ac035d1 ac035d4 ac035d5 ac035d6 ac035d7 ac035dno, result(r`wv'socyr) wave(`wv')
replace r`wv'socyr = 0 if ac035dno == 1 | ac035d1 == 0 | ac035d4 == 0 | ac035d5 == 0 | ac035d6 == 0 | ac035d7 == 0
replace r`wv'socyr = 1 if ac035d1 == 1 | ac035d4 == 1 | ac035d5 == 1 | ac035d6 == 1 | ac035d7 == 1
label variable r`wv'socyr "r`wv'socyr:w`wv' r any yearly social activities"
label values r`wv'socyr yrsoc

*wave 4 spouse any yearly social activites
gen s`wv'socyr = .
spouse r`wv'socyr, result(s`wv'socyr) wave(`wv')
label variable s`wv'socyr "s`wv'socyr:w`wv' s any yearly social activities"
label values s`wv'socyr yrsoc

****Frequency Participate Religious Groups***frequency not asked starting in wave 4

***Any Yearly Participation Religious Groups***change from previous waves
*wave 4 respondent any yearly religious groups
gen r`wv'relgyr = .
missing_common ac035d6 ac035dno, result(r`wv'relgyr) wave(`wv')
replace r`wv'relgyr = 0 if ac035dno == 1 | ac035d6 == 0 
replace r`wv'relgyr = 1 if ac035d6 == 1
label variable r`wv'relgyr "r`wv'relgyr:w`wv' r any yearly religious groups"
label values r`wv'relgyr yrsoc

*wave 4 spouse any yearly religious groups
gen s`wv'relgyr = .
spouse r`wv'relgyr, result(s`wv'relgyr) wave(`wv')
label variable s`wv'relgyr "s`wv'relgyr:w`wv' s any yearly religious groups"
label values s`wv'relgyr yrsoc



***drop wave 4 cover screen (all) file raw variables and non-repondent observations
drop `family_w4_cv'

***drop wave 4 cover screen (respondent) file raw variables
drop `family_w4_cv_r'

***drop wave 4 Demographic file raw variables
drop `family_w4_dn'

***drop wave 4 Children file raw variables
drop `family_w4_ch'

***drop wave 4 imputation file raw variables***
drop `family_w4_imput'

***drop wave 4 sp file raw variables***
drop `family_w4_sp'

***drop wave 4 ft raw variables***
drop `family_w4_ft'

***drop wave 4 ac raw variables***
drop `family_w4_ac'

***drop wave 4 ph raw variables***
drop `family_w4_ph'

***drop wave 4 sn raw variables***
drop `family_w4_sn'

***drop wave 4 techvar raw variables***
drop `family_w4_techvar'

******************************************************************************************

*set wave number
local wv = 5
local prewv = 4

***merge with wave 5 cover screen (respondent) data***
local family_w5_cv_r hhsize gender 
merge 1:1 mergeid using "$wave_5_cv_r", keepusing(`family_w5_cv_r') nogen

***merge with wave 5 Demographic data***
local family_w5_dn dn034_ dn036_ dn037_ dn026_1 dn026_2 dn027_1 dn027_2 dn028_1 dn028_2 ///
										dn032_1 dn032_2 dn030_1 dn030_2
merge 1:1 mergeid using "$wave_5_dn", keepusing(`family_w5_dn') nogen

***merge with wave 5 Children data***
local family_w5_ch ch001_ ch021_ ch005_* ch504_* ch007_* ch525d* ch526_* ch524_ ch014_*
merge 1:1 mergeid using "$wave_5_ch", keepusing(`family_w5_ch') nogen

***merge with wave 5 imputation data***
local family_w5_imput nchild nchild_f ngrchild ngrchild_f
merge 1:1 mergeid using "$wave_5_imputations_", keepusing(`family_w5_imput') nogen

***merge with wave 5 cover screen (all) data***
local family_w5_cv cvresp relrpers     
merge 1:1 mergeid using "$wave_5_cv_r", keepusing(`family_w5_cv') nogen

***merge with wave 5 sp data***
local family_w5_sp sp002_ sp003_1 sp003_2 sp003_3 sp008_ sp009_1 sp009_2 sp009_3 sp014_ sp018_ sp020_ ///
		sp019* sp021*
merge 1:1 mergeid using "$wave_5_sp", keepusing(`family_w5_sp') nogen

***merge with wave 5 ft data***
local family_w5_ft ft002_ ft003_* ft009_ ft010_* 
merge 1:1 mergeid using "$wave_5_ft", keepusing(`family_w5_ft') nogen

***merge with wave 5 ac data***
local family_w5_ac country language ac035d*
merge 1:1 mergeid using "$wave_5_ac", keepusing(`family_w5_ac') nogen

***merge with wave 5 physical health data***
local family_w5_ph ph048dno
merge 1:1 mergeid using "$wave_5_ph", keepusing(`family_w5_ph') nogen

***merge with wave 5 technical variables data***
local family_w5_techvar mn024_ mn101_
merge 1:1 mergeid using "$wave_5_techvar", keepusing(`family_w5_techvar') nogen


***parental mortality: mother alive***
*wave 5 respondent mother alive status
tempvar mominhouse
egen `mominhouse' = total(relrpers == 5 & gender == 2), by(hhid5)
recode `mominhouse' (1/10 = 1)

gen r5momliv = .
missing_common dn026_1, result(r5momliv) wave(`wv')
replace r5momliv = 0 if dn026_1 == 5 | ((r1momliv == 0 | r2momliv == 0 | r4momliv == 0) & inw5 == 1) 
replace r5momliv = 1 if (dn026_1 == 1) | ///
                        (`mominhouse' == 1 & cvresp == 1)
label variable r5momliv "r5momliv:w5 r mother alive"

*wave 5 spouse mother alive status
gen s5momliv = .
spouse r5momliv, result(s5momliv) wave(5)
label variable s5momliv "s5momliv:w5 s mother alive"
label values r5momliv liv
label values s5momliv liv

***parental mortality: father alive***
*wave 5 respondent father alive status
tempvar dadinhouse
egen `dadinhouse' = total(relrpers == 5 & gender == 1), by(hhid5)
recode `dadinhouse' (1/10 = 1)

gen r5dadliv = .
missing_common dn026_2, result(r5dadliv) wave(`wv')
replace r5dadliv = 0 if dn026_2 == 5 | ((r1dadliv == 0 | r2dadliv == 0 | r4dadliv == 0) & inw5 == 1)
replace r5dadliv = 1 if (dn026_2 == 1) | ///
                        (`dadinhouse' == 1 & cvresp == 1)
label variable r5dadliv "r5dadliv:w5 r father alive"

*wave 5 spouse father alive status
gen s5dadliv = .
spouse r5dadliv, result(s5dadliv) wave(5)
label variable s5dadliv "s5dadliv:w5 s father alive"
label values r5dadliv liv
label values s5dadliv liv

***parental mortality: mother's current age or age at death***
*wave 5 respondent mother's age
gen r5momage = .
missing_common dn026_1, result(r5momage) wave(`wv')
missing_common dn028_1 if dn026_1 == 1, result(r5momage) wave(`wv')
missing_common dn027_1 if dn026_1 == 5, result(r5momage) wave(`wv')
replace r5momage = .b if r4momage == .b & inw5 == 1
replace r5momage = r1momage if inrange(r1momage,1,120) & r1momliv == 0 & inw5 == 1
replace r5momage = r1momage + 8 if inrange(r1momage,1,120) & r1momliv == 1 & inw5==1 & r5momliv == 1
replace r5momage = r2momage if inrange(r2momage,1,120) & r2momliv == 0 & inw5 == 1
replace r5momage = r2momage + 6 if inrange(r2momage,1,120) & r2momliv == 1 & inw5==1 & r5momliv == 1
replace r5momage = r4momage if inrange(r4momage,1,120) & r4momliv == 0 & inw5 == 1
replace r5momage = r4momage + 2 if inrange(r4momage,1,120) & r4momliv == 1 & inw5==1 & r5momliv == 1
replace r5momage = dn028_1 if dn026_1 == 1 & inrange(dn028_1, 1, 120)
replace r5momage = dn027_1 if dn026_1 == 5 & inrange(dn027_1, 1, 120)
label variable r5momage "r5momage:w5 r mother age current/at death"

*wave 5 spouse mother's age
gen s5momage = .
spouse r5momage, result(s5momage) wave(5)
label variable s5momage "s5momage:w5 s mother age current/at death"

***parental mortality: father's current age or age at death***
*wave 5 respondent father's age
gen r5dadage = .
missing_common dn026_2, result(r5dadage) wave(`wv')
missing_common dn028_2 if dn026_2 == 1, result(r5dadage) wave(`wv')
missing_common dn027_2 if dn026_2 == 5, result(r5dadage) wave(`wv')
replace r5dadage = .b if r4dadage == .b & inw5 == 1
replace r5dadage = r1dadage if inrange(r1dadage,1,120) & r1dadliv == 0 & inw5 == 1
replace r5dadage = r1dadage + 8 if inrange(r1dadage,1,120) & r1dadliv == 1 & inw5==1 & r5dadliv == 1
replace r5dadage = r2dadage if inrange(r2dadage,1,120) & r2dadliv == 0 & inw5 == 1
replace r5dadage = r2dadage + 6 if inrange(r2dadage,1,120) & r2dadliv == 1 & inw5==1 & r5dadliv == 1
replace r5dadage = r4dadage if inrange(r4dadage,1,120) & r4dadliv == 0 & inw5 == 1
replace r5dadage = r4dadage + 2 if inrange(r4dadage,1,120) & r4dadliv == 1 & inw5==1 & r5dadliv == 1
replace r5dadage = dn028_2 if dn026_2 == 1 & inrange(dn028_2, 1, 120)
replace r5dadage = dn027_2 if dn026_2 == 5 & inrange(dn027_2, 1, 120)
label variable r5dadage "r5dadage:w5 r father age current/at death"

*wave 5 spouse father's age
gen s5dadage = .
spouse r5dadage, result(s5dadage) wave(5)
label variable s5dadage "s5dadage:w5 s father age current/at death"

***Number of people living in house***
*wave 5 household number of people living in house
gen hh5hhres = hhsize if inw5==1
label variable hh5hhres "hh5hhres:w5 Number of people in HH"

***Number of children***
*wave 5 household number of living children
gen h`wv'child = nchild
label variable h`wv'child "h`wv'child:w`wv' Number of living children R/P"

*wave 5 couple-level flag number of living children
chimputation_flag nchild_f, gen(h`wv'fchild) by(h`wv'coupid)
label variable h`wv'fchild "h`wv'fchild:w`wv' Impute Flag Num of living children"
label values h`wv'fchild fchild

***Number of grandchildren***
*wave 5 household number of grandchildren
gen h`wv'grchild = ngrchild
label variable h`wv'grchild "h`wv'grchild:w`wv' Number of grandchildren R/P"

*wave 5 couple-level flag number of grandchildren
chimputation_flag ngrchild_f, gen(h`wv'fgrchild) by(h`wv'coupid)
label variable h`wv'fgrchild "h`wv'fgrchild:w`wv' Impute Flag Num of grandchildren"
label values h`wv'fgrchild fchild

***Number of living siblings***
*wave 5 respondent number of living brothers
gen r5livbro =.
missing_common dn036_, result(r5livbro) wave(`wv')
replace r5livbro = r1livbro if !mi(r1livbro) & inw5==1
replace r5livbro = r2livbro if !mi(r2livbro) & inw5==1
replace r5livbro = r4livbro if !mi(r4livbro) & inw5==1
replace r5livbro = 0 if dn034_ == 5
replace r5livbro = dn036_ if inrange(dn036_,0,20)
label variable r5livbro "r5livbro:w5 r Number of living brothers"

*wave 5 respondent number of living sisters
gen r5livsis =.
missing_common dn037_, result(r5livsis) wave(`wv')
replace r5livsis = r1livsis if !mi(r1livsis) & inw5==1
replace r5livsis = r2livsis if !mi(r2livsis) & inw5==1
replace r5livsis = r4livsis if !mi(r4livsis) & inw5==1
replace r5livsis = 0 if dn034_ == 5
replace r5livsis = dn037_ if inrange(dn037_,0,20)
label variable r5livsis "r5livsis:w5 r Number of living sisters"

*wave 5 respondent number of living siblings
gen r5livsib =.
replace r5livsib = r5livbro + r5livsis
missing_h r5livbro r5livsis,result(r5livsib)
label variable r5livsib "r5livsib:w5 r Number of living siblings"

*wave 5 spouse number of living brothers
gen s5livbro =.
spouse r5livbro, result(s5livbro) wave(5)
label variable s5livbro "s5livbro:w5 s Number of living brothers"

*wave 5 spouse number of living sisters
gen s5livsis =.
spouse r5livsis, result(s5livsis) wave(5)
label variable s5livsis "s5livsis:w5 s Number of living sisters"

*wave 5 spouse number of living siblings
gen s5livsib =.
spouse r5livsib, result(s5livsib) wave(5)
label variable s5livsib "s5livsib:w5 s Number of living siblings"

***Number of living parents***
*wave 5 respondent number of living parents
gen r5livpar=.
missing_h r5momliv r5dadliv,result(r5livpar)
replace r5livpar = 0 if !missing(r5momliv) & !missing(r5dadliv)
replace r5livpar = r5livpar +1 if r5momliv==1 & !missing(r5dadliv)
replace r5livpar = r5livpar +1 if r5dadliv==1 & !missing(r5momliv)
label variable r5livpar "r5livpar:w5 r Number of living parents"

*wave 5 spouse number of living parents
gen s5livpar =.
spouse r5livpar, result(s5livpar) wave(5)
label variable s5livpar "s5livpar:w5 s Number of living parents"

***Number of living sons/daughters***
*wave 5 household number of living sons
recode ch005_* (-2 -1 2 = 0), prefix(m)
egen h`wv'son = rowtotal(mch005_*) if inw`wv' == 1,m 
replace h`wv'son = .m if (ch005_1==. | ch005_2==. | ch005_3==. | ch005_4==. | /// 
	ch005_5==. | ch005_6==. | ch005_7==. | ch005_8==. | ch005_9==. | ch005_10==. | ///
	ch005_11==. | ch005_12==. | ch005_13==. | ch005_14==. | ch005_15==. | ch005_16==. | ///
	ch005_17==. | ch005_18==. | ch005_19==. | ch005_20==.) & inlist(h`wv'son,.,0) & inw`wv' == 1
replace h`wv'son = .d if (ch005_1==-1 | ch005_2==-1 | ch005_3==-1 | ch005_4==-1 | /// 
	ch005_5==-1 | ch005_6==-1 | ch005_7==-1 | ch005_8==-1 | ch005_9==-1 | ch005_11==-1 | ///
	ch005_11==-1 | ch005_12==-1 | ch005_13==-1 | ch005_14==-1 | ch005_15==-1 | ch005_16==-1 | ///
	ch005_17==-1 | ch005_18==-1 | ch005_19==-1 | ch005_20==-1) & inlist(h`wv'son,.,.m,0)
replace h`wv'son = .r if (ch005_1==-2 | ch005_2==-2 | ch005_3==-2 | ch005_4==-2 | /// 
	ch005_5==-2 | ch005_6==-2 | ch005_7==-2 | ch005_8==-2 | ch005_9==-2 | ch005_11==-2 | ///
	ch005_11==-2 | ch005_12==-2 | ch005_13==-2 | ch005_14==-2 | ch005_15==-2 | ch005_16==-2 | ///
	ch005_17==-2 | ch005_18==-2 | ch005_19==-2 | ch005_20==-2) & inlist(h`wv'son,.,.m,0)
*wave 5 household number of living daughters
recode ch005_* (-2 -1 1 = 0) (2 = 1), prefix(f)
egen h`wv'dau = rowtotal(fch005_*) if inw`wv' == 1,m
replace h`wv'dau = .m if (ch005_1==. | ch005_2==. | ch005_3==. | ch005_4==. | /// 
	ch005_5==. | ch005_6==. | ch005_7==. | ch005_8==. | ch005_9==. | ch005_10==. | ///
	ch005_11==. | ch005_12==. | ch005_13==. | ch005_14==. | ch005_15==. | ch005_16==. | ///
	ch005_17==. | ch005_18==. | ch005_19==. | ch005_20==.) & inlist(h`wv'dau,.,0) & inw`wv' == 1
replace h`wv'dau = .d if (ch005_1==-1 | ch005_2==-1 | ch005_3==-1 | ch005_4==-1 | /// 
	ch005_5==-1 | ch005_6==-1 | ch005_7==-1 | ch005_8==-1 | ch005_9==-1 | ch005_11==-1 | ///
	ch005_11==-1 | ch005_12==-1 | ch005_13==-1 | ch005_14==-1 | ch005_15==-1 | ch005_16==-1 | ///
	ch005_17==-1 | ch005_18==-1 | ch005_19==-1 | ch005_20==-1) & inlist(h`wv'dau,.,.m,0)
replace h`wv'dau = .r if (ch005_1==-2 | ch005_2==-2 | ch005_3==-2 | ch005_4==-2 | /// 
	ch005_5==-2 | ch005_6==-2 | ch005_7==-2 | ch005_8==-2 | ch005_9==-2 | ch005_11==-2 | ///
	ch005_11==-2 | ch005_12==-2 | ch005_13==-2 | ch005_14==-2 | ch005_15==-2 | ch005_16==-2 | ///
	ch005_17==-2 | ch005_18==-2 | ch005_19==-2 | ch005_20==-2) & inlist(h`wv'dau,.,.m,0)
replace h`wv'son = 0 if h`wv'child == 0 | ((h`wv'child == h`wv'dau) & !mi(h`wv'dau))
replace h`wv'dau = 0 if h`wv'child == 0 | ((h`wv'child == h`wv'son) & !mi(h`wv'son))
label variable h`wv'son "h`wv'son:w`wv' Number of living sons R/P"
label variable h`wv'dau "h`wv'dau:w`wv' Number of living daughters R/P"

bysort h`wv'coupid: replace h`wv'son = h`wv'son[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'son = h`wv'son[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'dau = h`wv'dau[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'dau = h`wv'dau[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

drop mch005_* fch005_*

***Any Child Co-Resides***
*wave 5 household any child co-resides
gen h`wv'coresd = .
missing_common ch007_1 ch007_2 ch007_3 ch007_4 ch007_5 ch007_6 ch007_7 ch007_8 ch007_9 ch007_10 ch007_11 ch007_12 ch007_13 ch007_14 ///
	 ch007_15 ch007_16 ch007_17 ch007_18 ch007_19 ch007_20 ///
	 ch526_1 ch526_2 ch526_3 ch526_4 ch526_5 ch526_6 ch526_7 ch526_8 ch526_9 ch526_10 ch526_11 ch526_12 ch526_13 ch526_14 ch526_15 ///
	 ch526_16 ch526_17 ch526_18 ch526_19 ch526_20 nchild mn101_ ch524_, result(h`wv'coresd) wave(`wv')
replace h`wv'coresd = .k if nchild == 0
replace h`wv'coresd = h1coresd if inrange(h1coresd,0,1) & mn101_ == 1 & ch524_ == 5 & nchild != 0
replace h`wv'coresd = h2coresd if inrange(h2coresd,0,1) & mn101_ == 1 & ch524_ == 5 & nchild != 0
replace h`wv'coresd = h4coresd if inrange(h4coresd,0,1) & mn101_ == 1 & ch524_ == 5 & nchild != 0
replace h`wv'coresd = 0 if (nchild != 0 & (inrange(ch007_1,2,9) | inrange(ch007_2,2,9) | inrange(ch007_3,2,9) | inrange(ch007_4,2,9) | /// 
                                    	   inrange(ch007_5,2,9) | inrange(ch007_6,2,9) | inrange(ch007_7,2,9) | inrange(ch007_8,2,9) | ///
                                    	   inrange(ch007_9,2,9) | inrange(ch007_10,2,9) | inrange(ch007_11,2,9) | inrange(ch007_12,2,9) | ///
                                    	   inrange(ch007_13,2,9) | inrange(ch007_14,2,9) | inrange(ch007_15,2,9) | inrange(ch007_16,2,9) | ///
                                    	   inrange(ch007_17,2,9) | inrange(ch007_18,2,9) | inrange(ch007_19,2,9) | inrange(ch007_20,2,9))) | ///
		(nchild != 0 & mn101_ == 1 & ch524_ == 1 & (inrange(ch526_1,2,8) | inrange(ch526_2,2,8) | inrange(ch526_3,2,8) | inrange(ch526_4,2,8) | ///
		                                            inrange(ch526_5,2,8) | inrange(ch526_6,2,8) | inrange(ch526_7,2,8) | inrange(ch526_8,2,8) | ///
		                                            inrange(ch526_9,2,8) | inrange(ch526_10,2,8) | inrange(ch526_11,2,8) | inrange(ch526_12,2,8) | ///
		                                            inrange(ch526_13,2,8) | inrange(ch526_14,2,8) | inrange(ch526_15,2,8) | inrange(ch526_16,2,8) | ///
		                                            inrange(ch526_17,2,8) | inrange(ch526_18,2,8) | inrange(ch526_19,2,8) | inrange(ch526_10,2,8)))
replace h`wv'coresd = 1 if (nchild != 0 & (ch007_1 == 1 | ch007_2 == 1 | ch007_3 == 1 | ch007_4 == 1 | ch007_5 == 1 | ch007_6 == 1 | ///
                                    	   ch007_7 == 1 | ch007_8 == 1 | ch007_9 == 1 | ch007_10 == 1 | ch007_11 == 1 | ch007_12 == 1 | ///
                                    	   ch007_13 == 1 | ch007_14 == 1 | ch007_15 == 1 | ch007_16 == 1 | ch007_17 == 1 | ch007_18 == 1 | ///
                                    	   ch007_19 == 1 | ch007_20 == 1)) | ///
		(nchild != 0 & mn101_ == 1 & ch524_ == 1 & (ch526_1 == 1 | ch526_2 == 1 | ch526_3 == 1 | ch526_4 == 1 | ch526_5 == 1 | ///
		                                            ch526_6 == 1 | ch526_7 == 1 | ch526_8 == 1 | ch526_9 == 1 | ch526_10 == 1 | ///
		                                            ch526_11 == 1 | ch526_12 == 1 | ch526_13 == 1 | ch526_14 == 1 | ch526_15 == 1 | ///
		                                            ch526_16 == 1 | ch526_17 == 1 | ch526_18 == 1 | ch526_19 == 1 | ch526_20 == 1))
label variable h`wv'coresd "h`wv'coresd:w`wv' any child coresides with R/P"
label values h`wv'coresd child

list mn101_ ch001_ ch007_1 ch007_2  ch524_ ch526_1 ch526_2  if h5coresd==.m & r5famr == 1, noobs


bysort h`wv'coupid: replace h`wv'coresd = h`wv'coresd[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'coresd = h`wv'coresd[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Child Lives Near***
*wave 5 household any child lives near
gen h`wv'lvnear = .
missing_common ch007_1 ch007_2 ch007_3 ch007_4 ch007_5 ch007_6 ch007_7 ch007_8 ch007_9 ch007_10 ch007_11 ch007_12 ch007_13 ch007_14 ///
	 ch007_15 ch007_16 ch007_17 ch007_18 ch007_19 ch007_20 ch525d1 ch525d2 ch525d3 ch525d4 ch525d5 ch525d6 ch525d7 ch525d8 ///
	 ch525d9 ch525d10 ch525d11 ch525d12 ch525d13 ch525d14 ch525d15 ch525d16 ch525d17 ch525d18 ch525d19 ch525d20 ///
	 ch526_1 ch526_2 ch526_3 ch526_4 ch526_5 ch526_6 ch526_7 ch526_8 ch526_9 ch526_10 ch526_11 ch526_12 ch526_13 ch526_14 ch526_15 ///
	 ch526_16 ch526_17 ch526_18 ch526_19 ch526_20 nchild mn101_ ch524_, result(h`wv'lvnear) wave(`wv')
replace h`wv'lvnear = .k if nchild == 0
replace h`wv'lvnear = h`prewv'lvnear if inrange(h`prewv'lvnear,0,1) & mn101_ == 1 & ch524_ == 5 & nchild != 0
replace h`wv'lvnear = 0 if (nchild != 0 & (inrange(ch007_1,6,9) | inrange(ch007_2,6,9) | inrange(ch007_3,6,9) | inrange(ch007_4,6,9) | /// 
		inrange(ch007_5,6,9) | inrange(ch007_6,6,9) | inrange(ch007_7,6,9) | inrange(ch007_8,6,9) | inrange(ch007_9,6,9) | ///
		inrange(ch007_10,6,9) | inrange(ch007_11,6,9) | inrange(ch007_12,6,9) | inrange(ch007_13,6,9) | inrange(ch007_14,6,9) | ///
		inrange(ch007_15,6,9) | inrange(ch007_16,6,9) | inrange(ch007_17,6,9) | inrange(ch007_18,6,9) | inrange(ch007_19,6,9) | inrange(ch007_20,6,9))) | ///
		(nchild != 0 & mn101_ == 1 & ch524_ == 1 & ((ch525d1==1 & inrange(ch526_1,6,8)) | (ch525d2==1 & inrange(ch526_2,6,8)) | ///
		(ch525d3==1 & inrange(ch526_3,6,8)) | (ch525d4==1 & inrange(ch526_4,6,8)) | (ch525d5==1 & inrange(ch526_5,6,8)) | ///
		(ch525d6==1 & inrange(ch526_6,6,8)) | (ch525d7==1 & inrange(ch526_7,6,8)) | (ch525d8==1 & inrange(ch526_8,6,8)) | ///
		(ch525d9==1 & inrange(ch526_9,6,8)) | (ch525d10==1 & inrange(ch526_10,6,8)) | (ch525d11==1 & inrange(ch526_11,6,8)) | ///
		(ch525d12==1 & inrange(ch526_12,6,8)) | (ch525d13==1 & inrange(ch526_13,6,8)) | (ch525d14==1 & inrange(ch526_14,6,8)) | ///
		(ch525d15==1 & inrange(ch526_15,6,8)) | (ch525d16==1 & inrange(ch526_16,6,8)) | (ch525d17==1 & inrange(ch526_17,6,8)) | ///
		(ch525d18==1 & inrange(ch526_18,6,8)) | (ch525d19==1 & inrange(ch526_19,6,8)) | (ch525d20==1 & inrange(ch526_20,6,8))))
replace h`wv'lvnear = 1 if (nchild != 0 & (inrange(ch007_1,1,5) | inrange(ch007_2,1,5) | inrange(ch007_3,1,5) | inrange(ch007_4,1,5) | /// 
		inrange(ch007_5,1,5) | inrange(ch007_6,1,5) | inrange(ch007_7,1,5) | inrange(ch007_8,1,5) | inrange(ch007_9,1,5) | ///
		inrange(ch007_10,1,5) | inrange(ch007_11,1,5) | inrange(ch007_12,1,5) | inrange(ch007_13,1,5) | inrange(ch007_14,1,5) | ///
		inrange(ch007_15,1,5) | inrange(ch007_16,1,5) | inrange(ch007_17,1,5) | inrange(ch007_18,1,5) | inrange(ch007_19,1,5) | inrange(ch007_20,1,5))) | ///
		(nchild != 0 & mn101_ == 1 & ch524_ == 1 & ((ch525d1==1 & inrange(ch526_1,1,5)) | (ch525d2==1 & inrange(ch526_2,1,5)) | ///
		(ch525d3==1 & inrange(ch526_3,1,5)) | (ch525d4==1 & inrange(ch526_4,1,5)) | (ch525d5==1 & inrange(ch526_5,1,5)) | ///
		(ch525d6==1 & inrange(ch526_6,1,5)) | (ch525d7==1 & inrange(ch526_7,1,5)) | (ch525d8==1 & inrange(ch526_8,1,5)) | ///
		(ch525d9==1 & inrange(ch526_9,1,5)) | (ch525d10==1 & inrange(ch526_10,1,5)) | (ch525d11==1 & inrange(ch526_11,1,5)) | ///
		(ch525d12==1 & inrange(ch526_12,1,5)) | (ch525d13==1 & inrange(ch526_13,1,5)) | (ch525d14==1 & inrange(ch526_14,1,5)) | ///
		(ch525d15==1 & inrange(ch526_15,1,5)) | (ch525d16==1 & inrange(ch526_16,1,5)) | (ch525d17==1 & inrange(ch526_17,1,5)) | ///
		(ch525d18==1 & inrange(ch526_18,1,5)) | (ch525d19==1 & inrange(ch526_19,1,5)) | (ch525d20==1 & inrange(ch526_20,1,5))))
label variable h`wv'lvnear "h`wv'lvnear:w`wv' any child lives within 30 min"
label values h`wv'lvnear child

bysort h`wv'coupid: replace h`wv'lvnear = h`wv'lvnear[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'lvnear = h`wv'lvnear[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Weekly Contact with Children***
*wave 5 household any weekly contact with children
gen h`wv'kcnt = .
missing_common ch014_1 ch014_2 ch014_3 ch014_4 ch014_5 ch014_5 ch014_6 ch014_7 ch014_8 ch014_9 ch014_10 ///
		ch014_11 ch014_12 ch014_13 ch014_14 ch014_15 ch014_16 ch014_17 ch014_18 ch014_19 ch014_20 nchild, result(h`wv'kcnt) wave(`wv')
replace h`wv'kcnt = .d if h`wv'coresd == .d
replace h`wv'kcnt = .r if h`wv'coresd == .r
replace h`wv'kcnt = .k if nchild == 0
replace h`wv'kcnt = 0 if nchild != 0 & (inrange(ch014_1,4,7) | inrange(ch014_2,4,7) | inrange(ch014_3,4,7) | ///
		inrange(ch014_4,4,7) | inrange(ch014_5,4,7) | inrange(ch014_6,4,7) | inrange(ch014_7,4,7) | inrange(ch014_8,4,7) | inrange(ch014_9,4,7) | ///
		inrange(ch014_10,4,7) | inrange(ch014_11,4,7) | inrange(ch014_12,4,7) | inrange(ch014_13,4,7) | inrange(ch014_14,4,7) | inrange(ch014_15,4,7) | ///
		inrange(ch014_16,4,7) | inrange(ch014_17,4,7) | inrange(ch014_18,4,7) | inrange(ch014_19,4,7) | inrange(ch014_20,4,7))
replace h`wv'kcnt = 1 if nchild != 0 & (( h`wv'coresd == 1) | ///
		(inrange(ch014_1,1,3) | inrange(ch014_2,1,3) | inrange(ch014_3,1,3) | inrange(ch014_4,1,3) | inrange(ch014_5,1,3) | inrange(ch014_6,1,3) | ///
		inrange(ch014_7,1,3) | inrange(ch014_8,1,3) | inrange(ch014_9,1,3) | inrange(ch014_10,1,3) | inrange(ch014_11,1,3) | inrange(ch014_12,1,3) | ///
		inrange(ch014_13,1,3) | inrange(ch014_14,1,3) | inrange(ch014_15,1,3) | inrange(ch014_16,1,3) | inrange(ch014_17,1,3) | inrange(ch014_18,1,3) | ///
		inrange(ch014_19,1,3) | inrange(ch014_20,1,3)))
label variable h`wv'kcnt "h`wv'kcnt:w`wv' any weekly contact with children in person/phone/mail"
label values h`wv'kcnt contact

bysort h`wv'coupid: replace h`wv'kcnt = h`wv'kcnt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'kcnt = h`wv'kcnt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Frequency of Contact with Children***
*wave 5 household frequency contact with children
gen h`wv'kcntf = .
missing_common ch014_1 ch014_2 ch014_3 ch014_4 ch014_1 ch014_2 ch014_3 ch014_4 ch014_5 ch014_5 ch014_6 ch014_7 ///
 ch014_8 ch014_9 ch014_10 ch014_11 ch014_12 ch014_13 ch014_14 ch014_15 ch014_16 ch014_17 ch014_18 ch014_19 ch014_20 ///
 nchild, result(h`wv'kcntf) wave(`wv')
replace h`wv'kcntf = .d if h`wv'coresd == .d
replace h`wv'kcntf = .r if h`wv'coresd == .r
replace h`wv'kcntf = .k if nchild == 0
replace h`wv'kcntf = 7 if nchild != 0 & (ch014_1==7 | ch014_2==7 | ch014_3==7 | ch014_4==7 | ch014_5==7 | ch014_6==7 | ch014_7==7 | ///
		ch014_8==7 | ch014_9==7 | ch014_10==7 | ch014_11==7 | ch014_12==7 | ch014_13==7 | ch014_14==7 | ch014_15==7 | ch014_16==7 | ch014_17==7 | ///
		ch014_18==7 | ch014_19==7 | ch014_20==7)
replace h`wv'kcntf = 6 if nchild != 0 & (ch014_1==6 | ch014_2==6 | ch014_3==6 | ch014_4==6 | ch014_5==6 | ch014_6==6 | ch014_7==6 | ///
		ch014_8==6 | ch014_9==6 | ch014_10==6 | ch014_11==6 | ch014_12==6 | ch014_13==6 | ch014_14==6 | ch014_15==6 | ch014_16==6 | ch014_17==6 | ///
		ch014_18==6 | ch014_19==6 | ch014_20==6)
replace h`wv'kcntf = 5 if nchild != 0 & (ch014_1==5 | ch014_2==5 | ch014_3==5 | ch014_4==5 | ch014_5==5 | ch014_6==5 | ch014_7==5 | ///
		ch014_8==5 | ch014_9==5 | ch014_10==5 | ch014_11==5 | ch014_12==5 | ch014_13==5 | ch014_14==5 | ch014_15==5 | ch014_16==5 | ch014_17==5 | ///
		ch014_18==5 | ch014_19==5 | ch014_20==5)
replace h`wv'kcntf = 4 if nchild != 0 & (ch014_1==4 | ch014_2==4 | ch014_3==4 | ch014_4==4 | ch014_5==4 | ch014_6==4 | ch014_7==4 | ///
		ch014_8==4 | ch014_9==4 | ch014_10==4 | ch014_11==4 | ch014_12==4 | ch014_13==4 | ch014_14==4 | ch014_15==4 | ch014_16==4 | ch014_17==4 | ///
		ch014_18==4 | ch014_19==4 | ch014_20==4)
replace h`wv'kcntf = 3 if nchild != 0 & (ch014_1==3 | ch014_2==3 | ch014_3==3 | ch014_4==3 | ch014_5==3 | ch014_6==3 | ch014_7==3 | ///
		ch014_8==3 | ch014_9==3 | ch014_10==3 | ch014_11==3 | ch014_12==3 | ch014_13==3 | ch014_14==3 | ch014_15==3 | ch014_16==3 | ch014_17==3 | ///
		ch014_18==3 | ch014_19==3 | ch014_20==3)
replace h`wv'kcntf = 2 if nchild != 0 & (ch014_1==2 | ch014_2==2 | ch014_3==2 | ch014_4==2 | ch014_5==2 | ch014_6==2 | ch014_7==2 | ///
		ch014_8==2 | ch014_9==2 | ch014_10==2 | ch014_11==2 | ch014_12==2 | ch014_13==2 | ch014_14==2 | ch014_15==2 | ch014_16==2 | ch014_17==2 | ///
		ch014_18==2 | ch014_19==2 | ch014_20==2)
replace h`wv'kcntf = 1 if nchild != 0 & (h`wv'coresd == 1 | (ch014_1==1 | ch014_2==1 | ch014_3==1 | ch014_4==1 | ch014_5==1 | ch014_6==1 | ch014_7==1 | ///
		ch014_8==1 | ch014_9==1 | ch014_10==1 | ch014_11==1 | ch014_12==1 | ch014_13==1 | ch014_14==1 | ch014_15==1 | ch014_16==1 | ch014_17==1 | ///
		ch014_18==1 | ch014_19==1 | ch014_20==1))
label variable h`wv'kcntf "h`wv'kcntf:w`wv' frequency contact with children in person/phone/mail"
label values h`wv'kcntf freqcont

bysort h`wv'coupid: replace h`wv'kcntf = h`wv'kcntf[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'kcntf = h`wv'kcntf[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Weekly Contact with Parents***
*wave 5 respondent any weekly contact with parents
gen r`wv'pcnt = .
missing_common dn032_1 dn032_2 dn030_1 dn030_2, result(r`wv'pcnt) wave(`wv')
replace r`wv'pcnt = .n if r`wv'livpar == 0
replace r`wv'pcnt = 0 if r`wv'livpar != 0 & (inrange(dn032_1,4,7) | inrange(dn032_2,4,7))
replace r`wv'pcnt = 1 if r`wv'livpar != 0 & (inrange(dn032_1,1,3) | inrange(dn032_2,1,3) | dn030_1 == 1 | dn030_2 == 1) 
label variable r`wv'pcnt "r`wv'pcnt:w`wv' r any weekly contact with parents in person/phone/mail"
label values r`wv'pcnt contact

*wave 5 spouse any weekly contact with parents
gen s`wv'pcnt = .
spouse r`wv'pcnt, result(s`wv'pcnt) wave(`wv')
label variable s`wv'pcnt "s`wv'pcnt:w`wv' s any weekly contact with parents in person/phone/mail"
label values s`wv'pcnt contact

***Frequency of Contact with Parents***
*wave 5 respondent frequency contact with parents
gen r`wv'pcntf = .
missing_common dn032_1 dn032_2 dn030_1 dn030_2, result(r`wv'pcntf) wave(`wv')
replace r`wv'pcntf = .n if r`wv'livpar == 0
replace r`wv'pcntf = 7 if r`wv'livpar != 0 & (dn032_1 == 7 | dn032_2 == 7)
replace r`wv'pcntf = 6 if r`wv'livpar != 0 & (dn032_1 == 6 | dn032_2 == 6)
replace r`wv'pcntf = 5 if r`wv'livpar != 0 & (dn032_1 == 5 | dn032_2 == 5)
replace r`wv'pcntf = 4 if r`wv'livpar != 0 & (dn032_1 == 4 | dn032_2 == 4)
replace r`wv'pcntf = 3 if r`wv'livpar != 0 & (dn032_1 == 3 | dn032_2 == 3)
replace r`wv'pcntf = 2 if r`wv'livpar != 0 & (dn032_1 == 2 | dn032_2 == 2)
replace r`wv'pcntf = 1 if r`wv'livpar != 0 & (dn032_1 == 1 | dn032_2 == 1) | dn030_1 == 1 | dn030_2 == 1
label variable r`wv'pcntf "r`wv'pcntf:w`wv' r frequency contact with parents in person/phone/mail"
label values r`wv'pcntf freqcont

*wave 5 spouse frequency contact with parents
gen s`wv'pcntf = .
spouse r`wv'pcntf, result(s`wv'pcntf) wave(`wv')
label variable s`wv'pcntf "s`wv'pcntf:w`wv' s frequency contact with parents in person/phone/mail"
label values s`wv'pcntf freqcont

***Received Any Informal Care***
*wave 5 household received any informal care
gen hout = .
missing_common sp002_, result(hout) wave(`wv')
replace hout = 0 if sp002_ == 5
replace hout = 1 if sp002_ == 1
bysort h`wv'coupid: replace hout = hout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace hout = hout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rin = .
missing_common sp020_ hhsize ph048dno, result(rin) wave(`wv')
replace rin = 0 if sp020_ == 5  | hhsize == 1 | ph048dno == 1
replace rin = 1 if sp020_ == 1

gen spin = .
spouse rin, result(spin) wave(`wv')

gen h`wv'raany = .
replace h`wv'raany = .b if mi(spin) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .m if (hout == .m | rin == .m | spin == .m | r`wv'mstat == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .m if (hout == .m | rin == .m | r`wv'mstat == .m) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = .r if (hout == .r | rin == .r | spin == .r | r`wv'mstat == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .r if (hout == .r | rin == .r | r`wv'mstat == .r) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = .d if (hout == .d | rin == .d | spin == .d | r`wv'mstat == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .d if (hout == .d | rin == .d | r`wv'mstat == .d) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = 0 if (hout == 0 & rin == 0 & spin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = 0 if (hout == 0 & rin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = 1 if (hout == 1 | rin == 1 | spin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = 1 if (hout == 1 | rin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
label variable h`wv'raany "h`wv'raany:w`wv' received any informal care"
label values h`wv'raany carer

drop hout rin spin

***Received Informal Care from Children***
*wave 5 household received informal care from children
gen chout = .
missing_common sp002_ sp003_1 sp003_2 sp003_3 nchild, result(chout) wave(`wv')
replace chout = .k if nchild == 0
replace chout = 0 if nchild != 0 & (sp002_ == 5 | (sp002_ == 1 & ((inrange(sp003_1,1,9) | inrange(sp003_1,22,33)) | /// 
		(inrange(sp003_2,1,9) | inrange(sp003_2,22,33)) | (inrange(sp003_3,1,9) | inrange(sp003_3,22,33)))))
replace chout = 1 if nchild != 0 & (sp002_ == 1 & (inrange(sp003_1,10,21) | inrange(sp003_2,10,21) | inrange(sp003_3,10,21)))
bysort h`wv'coupid: replace chout = chout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace chout = chout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen crin = .
missing_common sp020_ sp021d10 sp021d11 sp021d12 sp021d13 sp021d14 sp021d15 sp021d16 sp021d17 sp021d18 sp021d19 ///
		sp021d20 sp021d21 hhsize ph048dno nchild, result(crin) wave(`wv')
replace crin = .k if nchild == 0
replace crin = 0 if hhsize == 1 | ph048dno == 1 | (nchild != 0 & (sp020_ == 5 | (sp020_ == 1 & (sp021d10 != 1 | sp021d11 != 1 | sp021d12 != 1 | ///
		sp021d13 != 1 | sp021d14 != 1 | sp021d15 != 1 | sp021d16 != 1 | sp021d17 != 1 | sp021d18 != 1 | sp021d19 != 1 | sp021d20 != 1 | sp021d21 != 1))))
replace crin = 1 if nchild != 0 & sp020_ == 1 & (sp021d10 == 1 | sp021d11 == 1 | sp021d12 == 1 | sp021d13 == 1 | ///
		sp021d14 == 1 | sp021d15 == 1 | sp021d16 == 1 | sp021d17 == 1 | sp021d18 == 1 | sp021d19 == 1 | sp021d20 == 1 | sp021d21 == 1)

gen cspin = .
spouse crin, result(cspin) wave (`wv')

gen h`wv'rccare = .
replace h`wv'rccare = .b if mi(cspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .m if (chout == .m | crin == .m | cspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .m if (chout == .m | crin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .r if (chout == .r | crin == .r | cspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .r if (chout == .r | crin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .d if (chout == .d | crin == .d | cspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .d if (chout == .d | crin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .k if (chout == .k | crin == .k | cspin == .k) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .k if (chout == .k | crin == .k) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = 0 if (chout == 0 & crin == 0 & cspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = 0 if (chout == 0 & crin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = 1 if (chout == 1 | crin == 1 | cspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = 1 if (chout == 1 | crin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
label variable h`wv'rccare "h`wv'rccare:w`wv' received informal care from children"
label values h`wv'rccare carer

drop chout crin cspin

***Received Informal Care from Relatives***
*wave 5 household received informal care from relatives
gen rhout = .
missing_common sp002_ sp003_1 sp003_2 sp003_3, result(rhout) wave(`wv')
replace rhout = 0 if sp002_ == 5 | (sp002_ == 1 & ((inrange(sp003_1,10,21) | inrange(sp003_1,29,33)) | /// 
		(inrange(sp003_2,1,9) | inrange(sp003_2,29,33)) | (inrange(sp003_3,1,9) | inrange(sp003_3,29,33))))
replace rhout = 1 if sp002_ == 1 & (inrange(sp003_1,1,9) | inrange(sp003_1,22,28) | inrange(sp003_2,1,9) | ///
		inrange(sp003_2,22,28) | inrange(sp003_3,1,9) | inrange(sp003_3,22,28))
bysort h`wv'coupid: replace rhout = rhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace rhout = rhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rrin = .
missing_common sp020_ sp021d1 sp021d2 sp021d3 sp021d4 sp021d5 sp021d6 sp021d7 sp021d8 sp021d9 sp021d22 sp021d23 sp021d24 sp021d25 ///
		sp021d26 sp021d27 sp021d28 hhsize ph048dno, result(rrin) wave(`wv')
replace rrin = 0 if hhsize == 1 | ph048dno == 1 | (sp020_ == 5 | (sp020_ == 1 & (sp021d1 != 1 | sp021d2 != 1 | sp021d3 != 1 | sp021d4 != 1 | ///
		sp021d5 != 1 | sp021d6 != 1 | sp021d7 != 1 | sp021d8 != 1 | sp021d9 != 1 | sp021d22 != 1 | sp021d23 != 1 | sp021d24 != 1 | ///
		sp021d25 != 1 | sp021d26 != 1 | sp021d27 != 1 | sp021d28 != 1)))
replace rrin = 1 if sp020_ == 1 & (sp021d1 == 1 | sp021d2 == 1 | sp021d3 == 1 | sp021d4 == 1 | ///
		sp021d5 == 1 | sp021d6 == 1 | sp021d7 == 1 | sp021d8 == 1 | sp021d9 == 1 | sp021d22 == 1 | sp021d23 == 1 | sp021d24 == 1 | ///
		sp021d25 == 1 | sp021d26 == 1 | sp021d27 == 1 | sp021d28 == 1)

gen rspin = .
spouse rrin, result(rspin) wave (`wv')

gen h`wv'rrcare = .
replace h`wv'rrcare = .b if mi(rspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .m if (rhout == .m | rrin == .m | rspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .m if (rhout == .m | rrin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = .r if (rhout == .r | rrin == .r | rspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .r if (rhout == .r | rrin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = .d if (rhout == .d | rrin == .d | rspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .d if (rhout == .d | rrin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = 0 if (rhout == 0 & rrin == 0 & rspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = 0 if (rhout == 0 & rrin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = 1 if (rhout == 1 | rrin == 1 | rspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = 1 if (rhout == 1 | rrin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
label variable h`wv'rrcare "h`wv'rrcare:w`wv' received informal care from relatives"
label values h`wv'rrcare carer

drop rhout rrin rspin

***Received Informal Care from non-relatives***
*wave 5 household received informal care from non-relatives
gen fhout = .
missing_common sp002_ sp003_1 sp003_2 sp003_3, result(fhout) wave(`wv')
replace fhout = 0 if sp002_ == 5 | (sp002_ == 1 & (inrange(sp003_1,1,28) | inrange(sp003_2,1,28) | inrange(sp003_3,1,28)))
replace fhout = 1 if sp002_ == 1 & (inrange(sp003_1,29,33) | inrange(sp003_2,29,33) | inrange(sp003_3,29,33))
bysort h`wv'coupid: replace fhout = fhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace fhout = fhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen frin = .
missing_common sp020_ sp021d29 sp021d30 sp021d31 sp021d32 sp021d33 hhsize ph048dno, result(frin) wave(`wv')
replace frin = 0 if hhsize == 1 | ph048dno == 1 | (sp020_ == 5 | (sp020_ == 1 & (sp021d29 != 1 | sp021d30 != 1 | sp021d31 != 1 | sp021d32 != 1 | ///
		sp021d33 != 1)))
replace frin = 1 if sp020_ == 1 & (sp021d29 == 1 | sp021d30 == 1 | sp021d31 == 1 | sp021d32 == 1 | ///
		sp021d33 == 1)

gen fspin = .
spouse frin, result(fspin) wave (`wv')

gen h`wv'rfcare = .
replace h`wv'rfcare = .b if mi(fspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .m if (fhout == .m | frin == .m | fspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .m if (fhout == .m | frin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = .r if (fhout == .r | frin == .r | fspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .r if (fhout == .r | frin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = .d if (fhout == .d | frin == .d | fspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .d if (fhout == .d | frin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = 0 if (fhout == 0 & frin == 0 & fspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = 0 if (fhout == 0 & frin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = 1 if (fhout == 1 | frin == 1 | fspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = 1 if (fhout == 1 | frin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
label variable h`wv'rfcare "h`wv'rfcare:w`wv' received informal care from non-relatives"
label values h`wv'rfcare carer

drop fhout frin fspin

***Gave Any Informal Care***
*wave 5 respondent gave any informal care 
gen hout = .
missing_common sp008_, result(hout) wave(`wv')
replace hout = 0 if sp008_ == 5
replace hout = 1 if sp008_ == 1
bysort h`wv'coupid: replace hout = hout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace hout = hout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rin = .
missing_common sp018_ hhsize, result(rin) wave(`wv')
replace rin = 0 if sp018_ == 5 | hhsize == 1
replace rin = 1 if sp018_ == 1

gen spin = .
spouse rin, result(spin) wave(`wv')

gen h`wv'gcaany = .
replace h`wv'gcaany = .b if mi(spin) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .m if (hout == .m | rin == .m | spin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .m if (hout == .m | rin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = .r if (hout == .r | rin == .r | spin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .r if (hout == .r | rin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = .d if (hout == .d | rin == .d | spin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .d if (hout == .d | rin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = 0 if (hout == 0 & rin == 0 & spin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = 0 if (hout == 0 & rin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = 1 if (hout == 1 | rin == 1 | spin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = 1 if (hout == 1 | rin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
label variable h`wv'gcaany "h`wv'gcaany:w`wv' gave any informal care"
label values h`wv'gcaany careg

drop hout rin spin

***Gave Informal Care to Children***
*wave 5 respondent gave informal care to children
gen chout = .
missing_common sp008_ sp009_1 sp009_2 sp009_3 nchild, result(chout) wave(`wv')
replace chout = .k if nchild == 0
replace chout = 0 if nchild != 0 & (sp008_ == 5 | (sp008_ == 1 & ((inrange(sp009_1,1,9) | /// 
		inrange(sp009_1,22,33)) | (inrange(sp009_2,1,9) | inrange(sp009_2,22,33)) | ///
		(inrange(sp009_3,1,9) | inrange(sp009_3,22,33)))))
replace chout = 1 if nchild != 0 & (sp008_ == 1 & (inrange(sp009_1,10,21) | inrange(sp009_2,10,21) | inrange(sp009_3,10,21)))
bysort h`wv'coupid: replace chout = chout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace chout = chout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen crin = .
missing_common sp018_ sp019d10 sp019d11 sp019d12 sp019d13 sp019d14 sp019d15 sp019d16 sp019d17 sp019d18 sp019d19 ///
		sp019d20 sp019d21 hhsize ph048dno nchild, result(crin) wave(`wv')
replace crin = .k if nchild == 0
replace crin = 0 if hhsize == 1 | ph048dno == 1 | (nchild != 0 & (sp018_ == 5 | (sp018_ == 1 & (sp019d10 != 1 | sp019d11 != 1 | sp019d12 != 1 | ///
		sp019d13 != 1 | sp019d14 != 1 | sp019d15 != 1 | sp019d16 != 1 | sp019d17 != 1 | sp019d18 != 1 | sp019d19 != 1 | sp019d20 != 1 | sp019d21 != 1))))
replace crin = 1 if nchild != 0 & sp018_ == 1 & (sp019d10 == 1 | sp019d11 == 1 | sp019d12 == 1 | sp019d13 == 1 | ///
		sp019d14 == 1 | sp019d15 == 1 | sp019d16 == 1 | sp019d17 == 1 | sp019d18 == 1 | sp019d19 == 1 | sp019d20 == 1 | sp019d21 == 1)

gen cspin = .
spouse crin, result(cspin) wave(`wv')

gen h`wv'gccare = .
replace h`wv'gccare = .b if mi(cspin) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .m if (chout == .m | crin == .m | cspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .m if (chout == .m | crin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .r if (chout == .r | crin == .r | cspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .r if (chout == .r | crin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .d if (chout == .d | crin == .d | cspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .d if (chout == .d | crin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .k if (chout == .k | crin == .k | cspin == .k) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .k if (chout == .k | crin == .k) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = 0 if (chout == 0 & crin == 0 & cspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = 0 if (chout == 0 & crin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = 1 if (chout == 1 | crin == 1 | cspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = 1 if (chout == 1 | crin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
label variable h`wv'gccare "h`wv'gccare:w`wv' gave informal care to children"
label values h`wv'gccare careg

drop chout crin cspin

***Gave Informal Care to Relatives***
*wave 5 respondent gave informal care to relatives
gen rhout = .
missing_common sp008_ sp009_1 sp009_2 sp009_3, result(rhout) wave(`wv')
replace rhout = 0 if sp008_ == 5 | (sp008_ == 1 & (inrange(sp009_1,10,21) | inrange(sp009_1,29,33) | inrange(sp009_2,10,21) | ///
		inrange(sp009_2,29,33) | inrange(sp009_3,10,21) | inrange(sp009_3,29,33)))
replace rhout = 1 if sp008_ == 1 & (inrange(sp009_1,1,9) | inrange(sp009_1,22,28) | inrange(sp009_2,1,9) | inrange(sp009_2,22,28) | ///
		inrange(sp009_3,1,9) | inrange(sp009_3,22,28))
bysort h`wv'coupid: replace rhout = rhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace rhout = rhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rrin = .
missing_common sp018_ sp019d1 sp019d2 sp019d3 sp019d4 sp019d5 sp019d6 sp019d7 sp019d8 sp019d9 ///
		sp019d23 sp019d24 sp019d25 sp019d26 sp019d27 sp019d28 hhsize ph048dno, result(rrin) wave(`wv')
replace rrin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & (sp019d1 != 1 | sp019d2 != 1 | sp019d3 != 1 | ///
		sp019d4 != 1 | sp019d5 != 1 | sp019d6 != 1 | sp019d7 != 1 | sp019d8 != 1 | sp019d9 != 1 | sp019d23 != 1 | sp019d24 != 1 | ///
		sp019d25 != 1 | sp019d26 != 1 | sp019d27 != 1 | sp019d28 != 1)))
replace rrin = 1 if sp018_ == 1 & (sp019d1 == 1 | sp019d2 == 1 | sp019d3 == 1 | sp019d4 == 1 | sp019d5 == 1 | sp019d6 == 1 | sp019d7 == 1 | ///
		sp019d8 == 1 | sp019d9 == 1 | sp019d23 == 1 | sp019d24 == 1 | sp019d25 == 1 | sp019d26 == 1 | sp019d27 == 1 | sp019d28 == 1)

gen rspin = .
spouse rrin, result(rspin) wave(`wv')

gen h`wv'grcare = .
replace h`wv'grcare = .b if mi(rspin) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .m if (rhout == .m | rrin == .m | rspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .m if (rhout == .m | rrin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = .r if (rhout == .r | rrin == .r | rspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .r if (rhout == .r | rrin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = .d if (rhout == .d | rrin == .d | rspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .d if (rhout == .d | rrin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = 0 if (rhout == 0 & rrin == 0 & rspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = 0 if (rhout == 0 & rrin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = 1 if (rhout == 1 | rrin == 1 | rspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = 1 if (rhout == 1 | rrin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
label variable h`wv'grcare "h`wv'grcare:w`wv' gave informal care to relatives"
label values h`wv'grcare careg

drop rhout rrin rspin

***Gave Informal Care to non-relatives***
*wave 5 respondent gave informal care to non-relatives
gen fhout = .
missing_common sp008_ sp009_1 sp009_2 sp009_3, result(fhout) wave(`wv')
replace fhout = 0 if sp008_ == 5 | (sp008_ == 1 & (inrange(sp009_1,1,28) | inrange(sp009_2,1,28) | inrange(sp009_3,1,28)))
replace fhout = 1 if sp008_ == 1 & (inrange(sp009_1,29,33) | inrange(sp009_2,29,33) | inrange(sp009_3,29,33))
bysort h`wv'coupid: replace fhout = fhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace fhout = fhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen frin = .
missing_common sp018_ sp019d29 sp019d30 sp019d31 sp019d32 sp019d33 hhsize ph048dno, result(frin) wave(`wv')
replace frin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & (sp019d29 != 1 | sp019d30 != 1 | sp019d31 != 1 | ///
		sp019d32 != 1 | sp019d33 != 1)))
replace frin = 1 if sp018_ == 1 & (sp019d29 == 1 | sp019d30 == 1 | sp019d31 == 1 | sp019d32 == 1 | sp019d33 == 1)

gen fspin = .
spouse frin, result(fspin) wave(`wv')

gen h`wv'gfcare = .
replace h`wv'gfcare = .b if mi(fspin) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .m if (fhout == .m | frin == .m | fspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .m if (fhout == .m | frin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = .r if (fhout == .r | frin == .r | fspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .r if (fhout == .r | frin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = .d if (fhout == .d | frin == .d | fspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .d if (fhout == .d | frin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = 0 if (fhout == 0 & frin == 0 & fspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = 0 if (fhout == 0 & frin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = 1 if (fhout == 1 | frin == 1 | fspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = 1 if (fhout == 1 | frin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
label variable h`wv'gfcare "h`wv'gfcare:w`wv' gave informal care to non-relatives"
label values h`wv'gfcare careg

drop fhout frin fspin

***Gave Informal Care to Grandchildren***
*wave 5 respondent gave care to grandchildren
gen khout = .
missing_common sp014_ ngrchild, result(khout) wave(`wv')
replace khout = .g if ngrchild == 0
replace khout = 0 if ngrchild != 0 & sp014_ == 5
replace khout = 1 if ngrchild != 0 & sp014_ == 1
bysort h`wv'coupid: replace khout = khout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace khout = khout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen krin = .
missing_common sp018_ sp019d22 hhsize ph048dno, result(krin) wave(`wv')
replace krin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & sp019d22 != 1))
replace krin = 1 if sp018_ == 1 & sp019d22 == 1

gen kspin = .
spouse krin, result(kspin) wave(`wv')

gen h`wv'gkcare = .
replace h`wv'gkcare = .b if mi(kspin) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .m if (khout == .m | krin == .m | kspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .m if (khout == .m | krin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .r if (khout == .r | krin == .r | kspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .r if (khout == .r | krin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .d if (khout == .d | krin == .d | kspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .d if (khout == .d | krin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .g if (khout == .g | krin == .g | kspin == .g) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .g if (khout == .g | krin == .g) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = 0 if (khout == 0 & krin == 0 & kspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = 0 if (khout == 0 & krin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = 1 if (khout == 1 | krin == 1 | kspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = 1 if (khout == 1 | krin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
label variable h`wv'gkcare "h`wv'gkcare:w`wv' gave care to grandchildren"
label values h`wv'gkcare careg

drop khout krin kspin

*bysort h`wv'coupid: replace h`wv'adlcare = h`wv'adlcare[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
*bysort h`wv'coupid: replace h`wv'adlcare = h`wv'adlcare[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Transfer to Children/Grandchildren***
*wave 5 household any transfer to children/grandchildren
gen h`wv'tcany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'tcany) wave(`wv')
replace h`wv'tcany = 0 if ft002_ == 5 | (ft002_ == 1 & ((inrange(ft003_1,1,9) | inrange(ft003_1,23,33) | /// 
														inrange(ft003_2,1,9) | inrange(ft003_2,23,33) | ///
														inrange(ft003_3,1,9) | inrange(ft003_3,23,33))))
replace h`wv'tcany = 1 if ft002_ == 1 & (inrange(ft003_1,10,22) | inrange(ft003_2,10,22) | inrange(ft003_3,10,22))
label variable h`wv'tcany "h`wv'tcany:w`wv' any transfer to children/grandchildren"
label values h`wv'tcany trans

bysort h`wv'coupid: replace h`wv'tcany = h`wv'tcany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tcany = h`wv'tcany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers to Parents***
*wave 5 household any transfers to parents
gen h`wv'tpany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'tpany) wave(`wv')
replace h`wv'tpany = 0 if ft002_ == 5 | (ft002_ == 1 & (ft003_1 == 1 | inrange(ft003_1,8,33) | ///
														ft003_2 == 1 | inrange(ft003_2,8,33) | ///
														ft003_3 == 1 | inrange(ft003_3,8,33)))
replace h`wv'tpany = 1 if ft002_ == 1 & (inrange(ft003_1,2,7) | inrange(ft003_2,2,7) | inrange(ft003_3,2,7))
label variable h`wv'tpany "h`wv'tpany:w`wv' any transfer to parents"
label values h`wv'tpany trans

bysort h`wv'coupid: replace h`wv'tpany = h`wv'tpany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tpany = h`wv'tpany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers to Other Relatives***
*wave 5 household any transfers to other relatives
gen h`wv'toany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'toany) wave(`wv')
replace h`wv'toany = 0 if ft002_ == 5 | (ft002_ == 1 & (inrange(ft003_1,2,7) | inrange(ft003_1,10,22) | inrange(ft003_1,29,33) | /// 
														inrange(ft003_2,2,7) | inrange(ft003_2,10,22) | inrange(ft003_2,29,33) | ///
														inrange(ft003_3,2,7) | inrange(ft003_3,10,22) | inrange(ft003_3,29,33)))
replace h`wv'toany = 1 if ft002_ == 1 & (inlist(ft003_1,1,8,9) | inrange(ft003_1,23,28) | inlist(ft003_2,1,8,9) | ///
		inrange(ft003_2,23,28) | inlist(ft003_3,1,8,9) | inrange(ft003_3,23,28))
label variable h`wv'toany "h`wv'toany:w`wv' any transfer to other relatives"
label values h`wv'toany trans

bysort h`wv'coupid: replace h`wv'toany = h`wv'toany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'toany = h`wv'toany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers to Other Non-Relatives***
*wave 5 household any transfers to other non-relatives
gen h`wv'tfany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'tfany) wave(`wv')
replace h`wv'tfany = 0 if ft002_ == 5 | (ft002_ == 1 & (inrange(ft003_1,1,28) | inrange(ft003_2,1,28) | inrange(ft003_3,1,28)))
replace h`wv'tfany = 1 if ft002_ == 1 & (inrange(ft003_1,29,33) | inrange(ft003_2,29,33) | inrange(ft003_3,29,33))
label variable h`wv'tfany "h`wv'tfany:w`wv' any transfer to other non-relatives"
label values h`wv'tfany trans

bysort h`wv'coupid: replace h`wv'tfany = h`wv'tfany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tfany = h`wv'tfany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers from Children/Grandchildren***
*wave 5 household any transfers from children/grandchildren
gen h`wv'fcany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'fcany) wave(`wv')
replace h`wv'fcany = 0 if ft009_ == 5 | (ft009_ == 1 & (inrange(ft010_1,1,9) | inrange(ft010_1,23,33) | ///
														inrange(ft010_2,1,9) | inrange(ft010_2,23,33) | ///
														inrange(ft010_3,1,9) | inrange(ft010_3,23,33)))
replace h`wv'fcany = 1 if ft009_ == 1 & (inrange(ft010_1,10,22) | inrange(ft010_2,10,22) | inrange(ft010_3,10,22))
label variable h`wv'fcany "h`wv'fcany:w`wv' any transfers from children/grandchildren"
label values h`wv'fcany trans

bysort h`wv'coupid: replace h`wv'fcany = h`wv'fcany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fcany = h`wv'fcany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers from Parents***
*wave 5 household any transfers from parents
gen h`wv'fpany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'fpany) wave(`wv')
replace h`wv'fpany = 0 if ft009_ == 5 | (ft009_ == 1 & (ft010_1 == 1 | inrange(ft010_1,8,33) | /// 
														ft010_2 == 1 | inrange(ft010_2,8,33) | ///
		 												ft010_3 == 1 | inrange(ft010_3,8,33)))
replace h`wv'fpany = 1 if ft009_ == 1 & (inrange(ft010_1,2,7) | inrange(ft010_2,2,7) | inrange(ft010_3,2,7))
label variable h`wv'fpany "h`wv'fpany:w`wv' any transfers from parents"
label values h`wv'fpany trans

bysort h`wv'coupid: replace h`wv'fpany = h`wv'fpany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fpany = h`wv'fpany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers from Other Relatives***
*wave 5 household any transfers from other relatives
gen h`wv'foany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'foany) wave(`wv')
replace h`wv'foany = 0 if ft009_ == 5 | (ft009_ == 1 & (inrange(ft010_1,2,7) | inrange(ft010_1,10,22) | inrange(ft010_1,29,33) | ///
	 													inrange(ft010_2,2,7) | inrange(ft010_2,10,22) | inrange(ft010_2,29,33) | ///
	 													inrange(ft010_3,2,7) | inrange(ft010_3,10,22) | inrange(ft010_3,29,33)))
replace h`wv'foany = 1 if ft009_ == 1 & (inlist(ft010_1,1,8,9) | inrange(ft010_1,23,28) | ///
										inlist(ft010_2,1,8,9) | inrange(ft010_2,23,28) | ///
										inlist(ft010_3,1,8,9) | inrange(ft010_3,23,28))
label variable h`wv'foany "h`wv'foany:w`wv' any transfers from other relatives"
label values h`wv'foany trans

bysort h`wv'coupid: replace h`wv'foany = h`wv'foany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'foany = h`wv'foany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers from Other Non-Relatives***
*wave 5 household any transfers from other non-relatives
gen h`wv'ffany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'ffany) wave(`wv')
replace h`wv'ffany = 0 if ft009_ == 5 | (ft009_ == 1 & (inrange(ft010_1,1,28) | inrange(ft010_2,1,28) |  inrange(ft010_3,1,28)))
replace h`wv'ffany = 1 if ft009_ == 1 & (inrange(ft010_1,29,33) | inrange(ft010_2,29,33) | inrange(ft010_3,29,33))
label variable h`wv'ffany "h`wv'ffany:w`wv' any transfers from other non-relatives"
label values h`wv'ffany trans

bysort h`wv'coupid: replace h`wv'ffany = h`wv'ffany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'ffany = h`wv'ffany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Social Activities in Past Year***changed from previous waves because frequency activities no longer asked
*wave 5 respondent any yearly social activities
gen r`wv'socyr = .
missing_common ac035d1 ac035d4 ac035d5 ac035d6 ac035d7 ac035dno, result(r`wv'socyr) wave(`wv')
replace r`wv'socyr = .n if mn024_==2
replace r`wv'socyr = 0 if ac035dno == 1 | ac035d1 == 0 | ac035d4 == 0 | ac035d5 == 0 | ac035d6 == 0 | ac035d7 == 0
replace r`wv'socyr = 1 if ac035d1 == 1 | ac035d4 == 1 | ac035d5 == 1 | ac035d6 == 1 | ac035d7 == 1
label variable r`wv'socyr "r`wv'socyr:w`wv' r any yearly social activities"
label values r`wv'socyr yrsoc

*wave 5 spouse any yearly social activites
gen s`wv'socyr = .
spouse r`wv'socyr, result(s`wv'socyr) wave(`wv')
label variable s`wv'socyr "s`wv'socyr:w`wv' s any yearly social activities"
label values s`wv'socyr yrsoc

***Any Yearly Participation Religious Groups***change from previous waves
*wave 5 respondent any yearly religious groups
gen r`wv'relgyr = .
missing_common ac035d6 ac035dno, result(r`wv'relgyr) wave(`wv')
replace r`wv'relgyr = .s if inlist(country,11,12,13,14,16,18,20,28,31,34) | (country == 25 & inlist(language,25,27)) | (country==23 & language == 24)
replace r`wv'relgyr = .n if mn024_==2
replace r`wv'relgyr = 0 if ac035dno == 1 | ac035d6 == 0 
replace r`wv'relgyr = 1 if ac035d6 == 1
label variable r`wv'relgyr "r`wv'relgyr:w`wv' r any yearly religious groups"
label values r`wv'relgyr yrsoc

*wave 5 spouse any yearly religious groups
gen s`wv'relgyr = .
spouse r`wv'relgyr, result(s`wv'relgyr) wave(`wv')
label variable s`wv'relgyr "s`wv'relgyr:w`wv' s any yearly religious groups"
label values s`wv'relgyr yrsoc


***drop wave 5 cover screen (all) file raw variables and non-respondent observations
drop `family_w5_cv'

***drop wave 5 cover screen (respondent) file raw variables
drop `family_w5_cv_r'

***drop wave 5 Demographic file raw variables
drop `family_w5_dn'

***drop wave 5 Children file raw variables
drop `family_w5_ch'

***drop wave 5 imputation file raw variables***
drop `family_w5_imput'

***drop wave 5 sp file raw variables
drop `family_w5_sp'

***drop wave 5 financial transfer file raw variables
drop `family_w5_ft'

***drop wave 5 activities file raw variables
drop `family_w5_ac'

***drop wave 5 ph raw variables***
drop `family_w5_ph'

***drop wave 5 technical variables file raw variables***
drop `family_w5_techvar'

******************************************************************************************

*set wave number
local wv = 6
local prewv = 5

***merge with wave 6 cover screen (respondent) data***
local family_w6_cv_r hhsize gender
merge 1:1 mergeid using "$wave_6_cv_r", keepusing(`family_w6_cv_r') nogen

***merge with wave 6 Demographic data***
local family_w6_dn dn034_ dn036_ dn037_ dn026_1 dn026_2 dn027_1 dn027_2 dn028_1 dn028_2 ///
										dn032_1 dn032_2 dn030_1 dn030_2
merge 1:1 mergeid using "$wave_6_dn", keepusing(`family_w6_dn') nogen

***merge with wave 6 Children data***
local family_w6_ch ch001_ ch021_ ch005_* ch007_* ch525d* ch526_* ch524_ ch014_*
merge 1:1 mergeid using "$wave_6_ch", keepusing(`family_w6_ch') nogen

***merge with wave 6 imputation data***
local family_w6_imput nchild nchild_f ngrchild ngrchild_f
merge 1:1 mergeid using "$wave_6_imputations_", keepusing(`family_w6_imput') nogen

***merge with wave 6 cover screen (all) data***
local family_w6_cv cvresp relrpers
merge 1:1 mergeid using "$wave_6_cv_r", keepusing(`family_w6_cv') nogen

***merge with wave 6 sp data***
local family_w6_sp sp002_ sp003_1 sp003_2 sp003_3 sp008_ sp009_1 sp009_2 sp009_3 sp014_ sp018_ sp020_ ///
		sp019* sp021*
merge 1:1 mergeid using "$wave_6_sp", keepusing(`family_w6_sp') nogen

***merge with wave 6 ft data***
local family_w6_ft ft002_ ft003_* ft009_ ft010_*
merge 1:1 mergeid using "$wave_6_ft", keepusing(`family_w6_ft') nogen

***merge with wave 6 ac data***
local family_w6_ac country language ac035d*
merge 1:1 mergeid using "$wave_6_ac", keepusing(`family_w6_ac') nogen

***merge with wave 6 physical health data***
local family_w6_ph ph048dno ph049dno 
merge 1:1 mergeid using "$wave_6_ph", keepusing(`family_w6_ph') nogen

***merge with wave 6 sn data***
local family_w6_sn sn005_* sn006_* sn007_*
merge 1:1 mergeid using "$wave_6_sn", keepusing(`family_w6_sn') nogen

***merge with wave 6 technical variables data***
local family_w6_techvar mn024_ mn101_ 
merge 1:1 mergeid using "$wave_6_techvar", keepusing(`family_w6_techvar') nogen


***parental mortality: mother alive***
*wave 6 respondent mother alive status
tempvar mominhouse
egen `mominhouse' = total(relrpers == 5 & gender == 2), by(hhid6)
recode `mominhouse' (1/10 = 1)

gen r6momliv = .
missing_common dn026_1, result(r6momliv) wave(`wv')
replace r6momliv = 0 if dn026_1 == 5 | ((r1momliv == 0 | r2momliv == 0 | r4momliv == 0 | r5momliv == 0) & inw6 == 1)
replace r6momliv = 1 if (dn026_1 == 1) | ///
                        (`mominhouse' == 1 & cvresp == 1)
label variable r6momliv "r6momliv:w6 r mother alive"

*wave 6 spouse mother alive status
gen s6momliv = .
spouse r6momliv, result(s6momliv) wave(6)
label variable s6momliv "s6momliv:w6 s mother alive"
label values r6momliv liv
label values s6momliv liv

***parental mortality: father alive***
*wave 6 respondent father alive status
tempvar dadinhouse
egen `dadinhouse' = total(relrpers == 5 & gender == 1), by(hhid6)
recode `dadinhouse' (1/10 = 1)

gen r6dadliv = .
missing_common dn026_2, result(r6dadliv) wave(`wv')
replace r6dadliv = 0 if dn026_2 == 5 | ((r1dadliv == 0 | r2dadliv == 0 | r4dadliv == 0 | r5dadliv == 0) & inw6 == 1)
replace r6dadliv = 1 if (dn026_2 == 1) | ///
                        (`dadinhouse' == 1 & cvresp == 1)
label variable r6dadliv "r6dadliv:w6 r father alive"

*wave 6 spouse father alive status
gen s6dadliv = .
spouse r6dadliv, result(s6dadliv) wave(6)
label variable s6dadliv "s6dadliv:w6 s father alive"
label values r6dadliv liv
label values s6dadliv liv

***parental mortality: mother's current age or age at death***
*wave 6 respondent mother's age
gen r6momage = .
missing_common dn026_1, result(r6momage) wave(`wv')
missing_common dn028_1 if dn026_1 == 1, result(r6momage) wave(`wv')
missing_common dn027_1 if dn026_1 == 5, result(r6momage) wave(`wv')
replace r6momage = .b if r4momage == .b & inw6 == 1
replace r6momage = r1momage if inrange(r1momage,1,120) & r1momliv == 0 & inw6 == 1
replace r6momage = r1momage + 8 if inrange(r1momage,1,120) & r1momliv == 1 & inw6==1 & r6momliv == 1
replace r6momage = r2momage if inrange(r2momage,1,120) & r2momliv == 0 & inw6 == 1
replace r6momage = r2momage + 6 if inrange(r2momage,1,120) & r2momliv == 1 & inw6==1 & r6momliv == 1
replace r6momage = r4momage if inrange(r4momage,1,120) & r4momliv == 0 & inw6 == 1
replace r6momage = r4momage + 4 if inrange(r4momage,1,120) & r4momliv == 1 & inw6==1 & r6momliv == 1
replace r6momage = r5momage if inrange(r5momage,1,120) & r5momliv == 0 & inw6 == 1
replace r6momage = r5momage + 2 if inrange(r5momage,1,120) & r5momliv == 1 & inw6==1 & r6momliv == 1
replace r6momage = dn028_1 if dn026_1 == 1 & inrange(dn028_1, 1, 120)
replace r6momage = dn027_1 if dn026_1 == 5 & inrange(dn027_1, 1, 120)
label variable r6momage "r6momage:w6 r mother age current/at death"

*wave 6 spouse mother's age
gen s6momage = .
spouse r6momage, result(s6momage) wave(6)
label variable s6momage "s6momage:w6 s mother age current/at death"

***parental mortality: father's current age or age at death***
*wave 6 respondent father's age
gen r6dadage = .
missing_common dn026_2, result(r6dadage) wave(`wv')
missing_common dn028_2 if dn026_2 == 1, result(r6dadage) wave(`wv')
missing_common dn027_2 if dn026_2 == 5, result(r6dadage) wave(`wv')
replace r6dadage = .b if r4dadage == .b & inw6 == 1
replace r6dadage = r1dadage if inrange(r1dadage,1,120) & r1dadliv == 0 & inw6 == 1
replace r6dadage = r1dadage + 8 if inrange(r1dadage,1,120) & r1dadliv == 1 & inw6==1 & r6dadliv == 1
replace r6dadage = r2dadage if inrange(r2dadage,1,120) & r2dadliv == 0 & inw6 == 1
replace r6dadage = r2dadage + 6 if inrange(r2dadage,1,120) & r2dadliv == 1 & inw6==1 & r6dadliv == 1
replace r6dadage = r4dadage if inrange(r4dadage,1,120) & r4dadliv == 0 & inw6 == 1
replace r6dadage = r4dadage + 4 if inrange(r4dadage,1,120) & r4dadliv == 1 & inw6==1 & r6dadliv == 1
replace r6dadage = r5dadage if inrange(r5dadage,1,120) & r5dadliv == 0 & inw6 == 1
replace r6dadage = r5dadage + 2 if inrange(r5dadage,1,120) & r5dadliv == 1 & inw6==1 & r6dadliv == 1
replace r6dadage = dn028_2 if dn026_2 == 1 & inrange(dn028_2, 1, 120)
replace r6dadage = dn027_2 if dn026_2 == 5 & inrange(dn027_2, 1, 120)
label variable r6dadage "r6dadage:w6 r father age current/at death"

*wave 6 spouse father's age
gen s6dadage = .
spouse r6dadage, result(s6dadage) wave(6)
label variable s6dadage "s6dadage:w6 s father age current/at death"

***Number of people living in house***
*wave 6 household number of people living in house
gen hh6hhres = hhsize if inw6==1
label variable hh6hhres "hh6hhres:w6 Number of people in HH"

***Number of children***
*wave 6 household number of living children
gen h`wv'child = nchild
label variable h`wv'child "h`wv'child:w`wv' Number of living children R/P"

*wave 6 couple-level flag number of living children
chimputation_flag nchild_f, gen(h`wv'fchild) by(h`wv'coupid)
label variable h`wv'fchild "h`wv'fchild:w`wv' Impute Flag Num of living children"
label values h`wv'fchild fchild

***Number of grandchildren***
*wave 6 household number of grandchildren
gen h`wv'grchild = ngrchild
label variable h`wv'grchild "h`wv'grchild:w`wv' Number of grandchildren R/P"

*wave 6 couple-level flag number of grandchildren
chimputation_flag ngrchild_f, gen(h`wv'fgrchild) by(h`wv'coupid)
label variable h`wv'fgrchild "h`wv'fgrchild:w`wv' Impute Flag Num of grandchildren"
label values h`wv'fgrchild fchild

***Number of living siblings***
*wave 6 respondent number of living brothers
gen r6livbro =.
missing_common dn036_, result(r6livbro) wave(`wv')
replace r6livbro = r1livbro if !mi(r1livbro) & inw6==1
replace r6livbro = r2livbro if !mi(r2livbro) & inw6==1
replace r6livbro = r4livbro if !mi(r4livbro) & inw6==1
replace r6livbro = r5livbro if !mi(r5livbro) & inw6==1
replace r6livbro = 0 if dn034_ == 5
replace r6livbro = dn036_ if inrange(dn036_,0,20)
label variable r6livbro "r6livbro:w6 r Number of living brothers"

*wave 6 respondent number of living sisters
gen r6livsis =.
missing_common dn037_, result(r6livsis) wave(`wv')
replace r6livsis = r1livsis if !mi(r1livsis) & inw6==1
replace r6livsis = r2livsis if !mi(r2livsis) & inw6==1
replace r6livsis = r4livsis if !mi(r4livsis) & inw6==1
replace r6livsis = r5livsis if !mi(r5livsis) & inw6==1
replace r6livsis = 0 if dn034_ == 5
replace r6livsis = dn037_ if inrange(dn037_,0,20)
label variable r6livsis "r6livsis:w6 r Number of living sisters"

*wave 6 respondent number of living siblings
gen r6livsib =.
replace r6livsib = r6livbro + r6livsis
missing_h r6livbro r6livsis,result(r6livsib)
label variable r6livsib "r6livsib:w6 r Number of living siblings"

*wave 6 spouse number of living brothers
gen s6livbro =.
spouse r6livbro, result(s6livbro) wave(6)
label variable s6livbro "s6livbro:w6 s Number of living brothers"

*wave 6 spouse number of living sisters
gen s6livsis =.
spouse r6livsis, result(s6livsis) wave(6)
label variable s6livsis "s6livsis:w6 s Number of living sisters"

*wave 6 spouse number of living siblings
gen s6livsib =.
spouse r6livsib, result(s6livsib) wave(6)
label variable s6livsib "s6livsib:w6 s Number of living siblings"

***Number of living parents***
*wave 6 respondent number of living parents
gen r6livpar=.
missing_h r6momliv r6dadliv,result(r6livpar)
replace r6livpar = 0 if !missing(r6momliv) & !missing(r6dadliv)
replace r6livpar = r6livpar +1 if r6momliv==1 & !missing(r6dadliv)
replace r6livpar = r6livpar +1 if r6dadliv==1 & !missing(r6momliv)
label variable r6livpar "r6livpar:w6 r Number of living parents"

*wave 6 spouse number of living parents
gen s6livpar =.
spouse r6livpar, result(s6livpar) wave(6)
label variable s6livpar "s6livpar:w6 s Number of living parents"

***Number of living sons/daughters***
*wave 6 household number of living sons
recode ch005_* (-2 -1 2 = 0), prefix(m)
egen h`wv'son = rowtotal(mch005_*) if inw`wv' == 1,m
replace h`wv'son = .m if (ch005_1==. | ch005_2==. | ch005_3==. | ch005_4==. | ///
	ch005_5==. | ch005_6==. | ch005_7==. | ch005_8==. | ch005_9==. | ch005_10==. | ///
	ch005_11==. | ch005_12==. | ch005_13==. | ch005_14==. | ch005_15==. | ch005_16==. | ///
	ch005_17==. | ch005_18==. | ch005_19==. | ch005_20==.) & inlist(h`wv'son,.,0) & inw`wv' == 1
replace h`wv'son = .d if (ch005_1==-1 | ch005_2==-1 | ch005_3==-1 | ch005_4==-1 | ///
	ch005_5==-1 | ch005_6==-1 | ch005_7==-1 | ch005_8==-1 | ch005_9==-1 | ch005_11==-1 | ///
	ch005_11==-1 | ch005_12==-1 | ch005_13==-1 | ch005_14==-1 | ch005_15==-1 | ch005_16==-1 | ///
	ch005_17==-1 | ch005_18==-1 | ch005_19==-1 | ch005_20==-1) & inlist(h`wv'son,.,.m,0)
replace h`wv'son = .r if (ch005_1==-2 | ch005_2==-2 | ch005_3==-2 | ch005_4==-2 | ///
	ch005_5==-2 | ch005_6==-2 | ch005_7==-2 | ch005_8==-2 | ch005_9==-2 | ch005_11==-2 | ///
	ch005_11==-2 | ch005_12==-2 | ch005_13==-2 | ch005_14==-2 | ch005_15==-2 | ch005_16==-2 | ///
	ch005_17==-2 | ch005_18==-2 | ch005_19==-2 | ch005_20==-2) & inlist(h`wv'son,.,.m,0)
*wave 6 household number of living daughters
recode ch005_* (-2 -1 1 = 0) (2 = 1), prefix(f)
egen h`wv'dau = rowtotal(fch005_*) if inw`wv' == 1,m
replace h`wv'dau = .m if (ch005_1==. | ch005_2==. | ch005_3==. | ch005_4==. | ///
	ch005_5==. | ch005_6==. | ch005_7==. | ch005_8==. | ch005_9==. | ch005_10==. | ///
	ch005_11==. | ch005_12==. | ch005_13==. | ch005_14==. | ch005_15==. | ch005_16==. | ///
	ch005_17==. | ch005_18==. | ch005_19==. | ch005_20==.) & inlist(h`wv'dau,.,0) & inw`wv' == 1
replace h`wv'dau = .d if (ch005_1==-1 | ch005_2==-1 | ch005_3==-1 | ch005_4==-1 | ///
	ch005_5==-1 | ch005_6==-1 | ch005_7==-1 | ch005_8==-1 | ch005_9==-1 | ch005_11==-1 | ///
	ch005_11==-1 | ch005_12==-1 | ch005_13==-1 | ch005_14==-1 | ch005_15==-1 | ch005_16==-1 | ///
	ch005_17==-1 | ch005_18==-1 | ch005_19==-1 | ch005_20==-1) & inlist(h`wv'dau,.,.m,0)
replace h`wv'dau = .r if (ch005_1==-2 | ch005_2==-2 | ch005_3==-2 | ch005_4==-2 | ///
	ch005_5==-2 | ch005_6==-2 | ch005_7==-2 | ch005_8==-2 | ch005_9==-2 | ch005_11==-2 | ///
	ch005_11==-2 | ch005_12==-2 | ch005_13==-2 | ch005_14==-2 | ch005_15==-2 | ch005_16==-2 | ///
	ch005_17==-2 | ch005_18==-2 | ch005_19==-2 | ch005_20==-2) & inlist(h`wv'dau,.,.m,0)

replace h`wv'son = 0 if h`wv'child == 0 | ((h`wv'child == h`wv'dau) & !mi(h`wv'dau))
replace h`wv'dau = 0 if h`wv'child == 0 | ((h`wv'child == h`wv'son) & !mi(h`wv'son))
label variable h`wv'son "h`wv'son:w`wv' Number of living sons R/P"
label variable h`wv'dau "h`wv'dau:w`wv' Number of living daughters R/P"

bysort h`wv'coupid: replace h`wv'son = h`wv'son[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'son = h`wv'son[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'dau = h`wv'dau[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'dau = h`wv'dau[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

drop mch005_* fch005_*


***Any Child Co-Resides***
*wave 6 household any child co-resides
gen h`wv'coresd = .
missing_common ch007_1 ch007_2 ch007_3 ch007_4 ch007_5 ch007_6 ch007_7 ch007_8 ch007_9 ch007_10 ch007_11 ch007_12 ch007_13 ch007_14 ///
	 ch007_15 ch007_16 ch007_17 ch007_18 ch007_19 ch007_20 ///
	 sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7 sn006_1 sn006_2 sn006_3 sn006_4 sn006_5 sn006_6 sn006_7 ///
	 ch526_1 ch526_2 ch526_3 ch526_4 ch526_5 ch526_6 ch526_7 ch526_8 ch526_9 ch526_10 ch526_11 ch526_12 ch526_13 ch526_14 ch526_15 ///
	 ch526_16 ch526_17 ch526_18 ch526_19 ch526_20 nchild mn101_ ch524_, result(h`wv'coresd)
replace h`wv'coresd = .k if nchild == 0
replace h`wv'coresd = h1coresd if inrange(h1coresd,0,1) & mn101_ == 1 & ch524_ == 5 & nchild != 0
replace h`wv'coresd = h2coresd if inrange(h2coresd,0,1) & mn101_ == 1 & ch524_ == 5 & nchild != 0
replace h`wv'coresd = h4coresd if inrange(h4coresd,0,1) & mn101_ == 1 & ch524_ == 5 & nchild != 0
replace h`wv'coresd = h5coresd if inrange(h5coresd,0,1) & mn101_ == 1 & ch524_ == 5 & nchild != 0
replace h`wv'coresd = 0 if (nchild != 0 & (inrange(ch007_1,2,8) | inrange(ch007_2,2,8) | inrange(ch007_3,2,8) | inrange(ch007_4,2,8) | /// 
                                    	   inrange(ch007_5,2,8) | inrange(ch007_6,2,8) | inrange(ch007_7,2,8) | inrange(ch007_8,2,8) | ///
                                    	   inrange(ch007_9,2,8) | inrange(ch007_10,2,8) | inrange(ch007_11,2,8) | inrange(ch007_12,2,8) | ///
                                    	   inrange(ch007_13,2,8) | inrange(ch007_14,2,8) | inrange(ch007_15,2,8) | inrange(ch007_16,2,8) | ///
                                    	   inrange(ch007_17,2,8) | inrange(ch007_18,2,8) | inrange(ch007_19,2,8) | inrange(ch007_20,2,8))) | ///
					(nchild != 0 & ((inrange(sn005_1,1,9) | inrange(sn005_1,14,96) | inrange(sn005_2,1,9) | inrange(sn005_2,14,96) | inrange(sn005_3,1,9) | ///
    								inrange(sn005_3,14,96) | inrange(sn005_4,1,9) | inrange(sn005_4,14,96) | inrange(sn005_5,1,9) | inrange(sn005_5,14,96) | ///
    								inrange(sn005_6,1,9) | inrange(sn005_6,14,96) | inrange(sn005_7,1,9) | inrange(sn005_7,14,96)) | ///
    								(inrange(sn006_1,2,8) | inrange(sn006_2,2,8) | inrange(sn006_3,2,8) | inrange(sn006_4,2,8) | inrange(sn006_5,2,8) | ///
    								inrange(sn006_6,2,8) | inrange(sn006_7,2,8)))) | ///                      	   
					(nchild != 0 & mn101_ == 1 & ch524_ == 1 & (inrange(ch526_1,2,8) | inrange(ch526_2,2,8) | inrange(ch526_3,2,8) | inrange(ch526_4,2,8) | ///
		                                            inrange(ch526_5,2,8) | inrange(ch526_6,2,8) | inrange(ch526_7,2,8) | inrange(ch526_8,2,8) | ///
		                                            inrange(ch526_9,2,8) | inrange(ch526_10,2,8) | inrange(ch526_11,2,8) | inrange(ch526_12,2,8) | ///
		                                            inrange(ch526_13,2,8) | inrange(ch526_14,2,8) | inrange(ch526_15,2,8) | inrange(ch526_16,2,8) | ///
		                                            inrange(ch526_17,2,8) | inrange(ch526_18,2,8) | inrange(ch526_19,2,8) | inrange(ch526_10,2,8)))
replace h`wv'coresd = 1 if (nchild != 0 & (ch007_1 == 1 | ch007_2 == 1 | ch007_3 == 1 | ch007_4 == 1 | ch007_5 == 1 | ch007_6 == 1 | ///
                                    	   ch007_7 == 1 | ch007_8 == 1 | ch007_9 == 1 | ch007_10 == 1 | ch007_11 == 1 | ch007_12 == 1 | ///
                                    	   ch007_13 == 1 | ch007_14 == 1 | ch007_15 == 1 | ch007_16 == 1 | ch007_17 == 1 | ch007_18 == 1 | ///
                                    	   ch007_19 == 1 | ch007_20 == 1)) | ///
    (nchild != 0 & ((inrange(sn005_1,10,13) & sn006_1 == 1) | (inrange(sn005_2,10,13) & sn006_2 == 1) | (inrange(sn005_3,10,13) & sn006_3 == 1) | ///
    					(inrange(sn005_4,10,13) & sn006_4 == 1) | (inrange(sn005_5,10,13) & sn006_5 == 1) | (inrange(sn005_6,10,13) & sn006_6 == 1) | ///
    					(inrange(sn005_7,10,13) & sn006_7 == 1))) | ///
		(nchild != 0 & mn101_ == 1 & ch524_ == 1 & (ch526_1 == 1 | ch526_2 == 1 | ch526_3 == 1 | ch526_4 == 1 | ch526_5 == 1 | ///
		                                            ch526_6 == 1 | ch526_7 == 1 | ch526_8 == 1 | ch526_9 == 1 | ch526_10 == 1 | ///
		                                            ch526_11 == 1 | ch526_12 == 1 | ch526_13 == 1 | ch526_14 == 1 | ch526_15 == 1 | ///
		                                            ch526_16 == 1 | ch526_17 == 1 | ch526_18 == 1 | ch526_19 == 1 | ch526_20 == 1))
label variable h`wv'coresd "h`wv'coresd:w`wv' any child coresides with R/P"
label values h`wv'coresd child

*list mn101_ ch001_ ch007_1 ch007_2  ch524_ ch526_1 ch526_2  if h6coresd==.m & r6famr == 1, noobs


bysort h`wv'coupid: replace h`wv'coresd = h`wv'coresd[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'coresd = h`wv'coresd[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Child Lives Near***
*wave 6 household any child lives near
gen h`wv'lvnear = .
missing_common ch007_1 ch007_2 ch007_3 ch007_4 ch007_5 ch007_6 ch007_7 ch007_8 ch007_9 ch007_10 ch007_11 ch007_12 ch007_13 ch007_14 ///
	 sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7 sn006_1 sn006_2 sn006_3 sn006_4 sn006_5 sn006_6 sn006_7 ///
	 ch007_15 ch007_16 ch007_17 ch007_18 ch007_19 ch007_20 ch525d1 ch525d2 ch525d3 ch525d4 ch525d5 ch525d6 ch525d7 ch525d8 ///
	 ch525d9 ch525d10 ch525d11 ch525d12 ch525d13 ch525d14 ch525d15 ch525d16 ch525d17 ch525d18 ch525d19 ch525d20 ///
	 ch526_1 ch526_2 ch526_3 ch526_4 ch526_5 ch526_6 ch526_7 ch526_8 ch526_9 ch526_10 ch526_11 ch526_12 ch526_13 ch526_14 ch526_15 ///
	 ch526_16 ch526_17 ch526_18 ch526_19 ch526_20 nchild mn101_ ch524_, result(h`wv'lvnear)
replace h`wv'lvnear = .k if nchild == 0
replace h`wv'lvnear = h`prewv'lvnear if inrange(h`prewv'lvnear,0,1) & mn101_ == 1 & ch524_ == 5 & nchild != 0
replace h`wv'lvnear = 0 if (nchild != 0 & (inrange(ch007_1,6,9) | inrange(ch007_2,6,9) | inrange(ch007_3,6,8) | inrange(ch007_4,6,8) | /// 
			inrange(ch007_5,6,8) | inrange(ch007_6,6,8) | inrange(ch007_7,6,8) | inrange(ch007_8,6,8) | inrange(ch007_9,6,8) | ///
			inrange(ch007_10,6,8) | inrange(ch007_11,6,8) | inrange(ch007_12,6,8) | inrange(ch007_13,6,8) | inrange(ch007_14,6,8) | ///
			inrange(ch007_15,6,8) | inrange(ch007_16,6,8) | inrange(ch007_17,6,8) | inrange(ch007_18,6,8) | inrange(ch007_19,6,8) | inrange(ch007_20,6,8))) | ///
		(nchild != 0 & ((inrange(sn005_1,1,9) | inrange(sn005_1,14,96) | inrange(sn005_2,1,9) | inrange(sn005_2,14,96) | inrange(sn005_3,1,9) | ///
			inrange(sn005_3,14,96) | inrange(sn005_4,1,9) | inrange(sn005_4,14,96) | inrange(sn005_5,1,9) | inrange(sn005_5,14,96) | ///
			inrange(sn005_6,1,9) | inrange(sn005_6,1,9) | inrange(sn005_7,1,9) | inrange(sn005_7,1,9)) | inrange(sn006_1,6,8) | inrange(sn006_2,6,8) | ///
			inrange(sn006_3,6,8) | inrange(sn006_4,6,8) | inrange(sn006_5,6,8) | inrange(sn006_6,6,8) | inrange(sn006_7,6,8))) | ///
		(nchild != 0 & mn101_ == 1 & ch524_ == 1 & ((ch525d1==1 & inrange(ch526_1,6,8)) | (ch525d2==1 & inrange(ch526_2,6,8)) | ///
			(ch525d3==1 & inrange(ch526_3,6,8)) | (ch525d4==1 & inrange(ch526_4,6,8)) | (ch525d5==1 & inrange(ch526_5,6,8)) | ///
			(ch525d6==1 & inrange(ch526_6,6,8)) | (ch525d7==1 & inrange(ch526_7,6,8)) | (ch525d8==1 & inrange(ch526_8,6,8)) | ///
			(ch525d9==1 & inrange(ch526_9,6,8)) | (ch525d10==1 & inrange(ch526_10,6,8)) | (ch525d11==1 & inrange(ch526_11,6,8)) | ///
			(ch525d12==1 & inrange(ch526_12,6,8)) | (ch525d13==1 & inrange(ch526_13,6,8)) | (ch525d14==1 & inrange(ch526_14,6,8)) | ///
			(ch525d15==1 & inrange(ch526_15,6,8)) | (ch525d16==1 & inrange(ch526_16,6,8)) | (ch525d17==1 & inrange(ch526_17,6,8)) | ///
			(ch525d18==1 & inrange(ch526_18,6,8)) | (ch525d19==1 & inrange(ch526_19,6,8)) | (ch525d20==1 & inrange(ch526_20,6,8))))
replace h`wv'lvnear = 1 if (nchild != 0 & (inrange(ch007_1,1,5) | inrange(ch007_2,1,5) | inrange(ch007_3,1,5) | inrange(ch007_4,1,5) | /// 
			inrange(ch007_5,1,5) | inrange(ch007_6,1,5) | inrange(ch007_7,1,5) | inrange(ch007_8,1,5) | inrange(ch007_9,1,5) | ///
			inrange(ch007_10,1,5) | inrange(ch007_11,1,5) | inrange(ch007_12,1,5) | inrange(ch007_13,1,5) | inrange(ch007_14,1,5) | ///
			inrange(ch007_15,1,5) | inrange(ch007_16,1,5) | inrange(ch007_17,1,5) | inrange(ch007_18,1,5) | inrange(ch007_19,1,5) | inrange(ch007_20,1,5))) | ///
		(nchild != 0 & ((inrange(sn005_1,10,13) & inrange(sn006_1,1,5)) | (inrange(sn005_2,10,13) & inrange(sn006_2,1,5)) | ///
			(inrange(sn005_3,10,13) & inrange(sn006_3,1,5)) | (inrange(sn005_4,10,13) & inrange(sn006_4,1,5)) | (inrange(sn005_5,10,13) & inrange(sn006_5,1,5)) | ///
			(inrange(sn005_6,10,13) & inrange(sn006_6,1,5)) | (inrange(sn005_7,10,13) & inrange(sn006_7,1,5)))) | ///
		(nchild != 0 & mn101_ == 1 & ch524_ == 1 & ((ch525d1==1 & inrange(ch526_1,1,5)) | (ch525d2==1 & inrange(ch526_2,1,5)) | ///
			(ch525d3==1 & inrange(ch526_3,1,5)) | (ch525d4==1 & inrange(ch526_4,1,5)) | (ch525d5==1 & inrange(ch526_5,1,5)) | ///
			(ch525d6==1 & inrange(ch526_6,1,5)) | (ch525d7==1 & inrange(ch526_7,1,5)) | (ch525d8==1 & inrange(ch526_8,1,5)) | ///
			(ch525d9==1 & inrange(ch526_9,1,5)) | (ch525d10==1 & inrange(ch526_10,1,5)) | (ch525d11==1 & inrange(ch526_11,1,5)) | ///
			(ch525d12==1 & inrange(ch526_12,1,5)) | (ch525d13==1 & inrange(ch526_13,1,5)) | (ch525d14==1 & inrange(ch526_14,1,5)) | ///
			(ch525d15==1 & inrange(ch526_15,1,5)) | (ch525d16==1 & inrange(ch526_16,1,5)) | (ch525d17==1 & inrange(ch526_17,1,5)) | ///
			(ch525d18==1 & inrange(ch526_18,1,5)) | (ch525d19==1 & inrange(ch526_19,1,5)) | (ch525d20==1 & inrange(ch526_20,1,5))))
label variable h`wv'lvnear "h`wv'lvnear:w`wv' any child lives within 30 min"
label values h`wv'lvnear child

bysort h`wv'coupid: replace h`wv'lvnear = h`wv'lvnear[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'lvnear = h`wv'lvnear[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Weekly Contact with Children***
*wave 6 household any weekly contact with children
gen h`wv'kcnt = .
missing_common sn007_1 sn007_2 sn007_3 sn007_4 sn007_5 sn007_6 sn007_7 ch014_1 ch014_2 ch014_3 ch014_4 ch014_5 ch014_5 ch014_6 ///
ch014_7 ch014_8 ch014_9 ch014_10 ch014_11 ch014_12 ch014_13 ch014_14 ch014_15 ch014_16 ch014_17 ch014_18 ch014_19 ch014_20 nchild, result(h`wv'kcnt)
replace h`wv'kcnt = .d if h`wv'coresd == .d
replace h`wv'kcnt = .r if h`wv'coresd == .r
replace h`wv'kcnt = .k if nchild == 0
replace h`wv'kcnt = 0 if nchild != 0 & (inrange(sn007_1,4,7) | inrange(sn007_2,4,7) | inrange(sn007_3,4,7) | inrange(sn007_4,4,7) | ///
										inrange(sn007_5,4,7) | inrange(sn007_6,4,7) | inrange(sn007_7,4,7)) | ///
		(inrange(ch014_1,4,7) | inrange(ch014_2,4,7) | inrange(ch014_3,4,7) | inrange(ch014_4,4,7) | inrange(ch014_5,4,7) | ///
		inrange(ch014_6,4,7) | inrange(ch014_7,4,7) | inrange(ch014_8,4,7) | inrange(ch014_9,4,7) |inrange(ch014_10,4,7) | ///
		inrange(ch014_11,4,7) | inrange(ch014_12,4,7) | inrange(ch014_13,4,7) | inrange(ch014_14,4,7) | inrange(ch014_15,4,7) | ///
		inrange(ch014_16,4,7) | inrange(ch014_17,4,7) | inrange(ch014_18,4,7) | inrange(ch014_19,4,7) | inrange(ch014_20,4,7))
replace h`wv'kcnt = 1 if nchild != 0 & ((inrange(sn007_1,1,3) & inrange(sn005_1,10,13)) | (inrange(sn007_2,1,3) & inrange(sn005_2,10,13)) | /// 
		(inrange(sn007_3,1,3) & inrange(sn005_3,10,13)) | (inrange(sn007_4,1,3) & inrange(sn005_4,10,13)) | (inrange(sn007_5,1,3) & inrange(sn005_5,10,13)) | ///
		(inrange(sn007_6,1,3) & inrange(sn005_6,10,13)) | (inrange(sn007_7,1,3) & inrange(sn005_7,10,13)) | ///
		h`wv'coresd == 1) | ///
		(inrange(ch014_1,1,3) | inrange(ch014_2,1,3) | inrange(ch014_3,1,3) | inrange(ch014_4,1,3) | inrange(ch014_5,1,3) | inrange(ch014_6,1,3) | ///
		inrange(ch014_7,1,3) | inrange(ch014_8,1,3) | inrange(ch014_9,1,3) | inrange(ch014_10,1,3) | inrange(ch014_11,1,3) | inrange(ch014_12,1,3) | ///
		inrange(ch014_13,1,3) | inrange(ch014_14,1,3) | inrange(ch014_15,1,3) | inrange(ch014_16,1,3) | inrange(ch014_17,1,3) | inrange(ch014_18,1,3) | ///
		inrange(ch014_19,1,3) | inrange(ch014_20,1,3))
label variable h`wv'kcnt "h`wv'kcnt:w`wv' any weekly contact with children in person/phone/mail"
label values h`wv'kcnt contact

bysort h`wv'coupid: replace h`wv'kcnt = h`wv'kcnt[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'kcnt = h`wv'kcnt[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Frequency of Contact with Children***
*wave 6 household frequency contact with children
gen h`wv'kcntf = .
missing_common ch014_1 ch014_2 ch014_3 ch014_4 ch014_1 ch014_2 ch014_3 ch014_4 ch014_5 ch014_5 ch014_6 ch014_7 ///
 ch014_8 ch014_9 ch014_10 ch014_11 ch014_12 ch014_13 ch014_14 ch014_15 ch014_16 ch014_17 ch014_18 ch014_19 ch014_20 ///
 sn005_1 sn005_2 sn005_3 sn005_4 sn005_5 sn005_6 sn005_7 sn007_1 sn007_2 sn007_3 sn007_4 sn007_5 sn007_6 sn007_7 nchild, result(h`wv'kcntf)
replace h`wv'kcntf = .d if h`wv'coresd == .d
replace h`wv'kcntf = .r if h`wv'coresd == .r
replace h`wv'kcntf = .k if nchild == 0
replace h`wv'kcntf = 7 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 7) | (inrange(sn005_2,10,13) & sn007_2 == 7) | (inrange(sn005_3,10,13) & sn007_3 == 7) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 7) | (inrange(sn005_5,10,13) & sn007_5 == 7) | (inrange(sn005_6,10,13) & sn007_6 == 7) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 7)) | ///
		(ch014_1==7 | ch014_2==7 | ch014_3==7 | ch014_4==7 | ch014_5==7 | ch014_6==7 | ch014_7==7 | ///
		ch014_8==7 | ch014_9==7 | ch014_10==7 | ch014_11==7 | ch014_12==7 | ch014_13==7 | ch014_14==7 | ch014_15==7 | ch014_16==7 | ch014_17==7 | ///
		ch014_18==7 | ch014_19==7 | ch014_20==7)
replace h`wv'kcntf = 6 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 6) | (inrange(sn005_2,10,13) & sn007_2 == 6) | (inrange(sn005_3,10,13) & sn007_3 == 6) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 6) | (inrange(sn005_5,10,13) & sn007_5 == 6) | (inrange(sn005_6,10,13) & sn007_6 == 6) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 6)) | ///
		(ch014_1==6 | ch014_2==6 | ch014_3==6 | ch014_4==6 | ch014_5==6 | ch014_6==6 | ch014_7==6 | ///
		ch014_8==6 | ch014_9==6 | ch014_10==6 | ch014_11==6 | ch014_12==6 | ch014_13==6 | ch014_14==6 | ch014_15==6 | ch014_16==6 | ch014_17==6 | ///
		ch014_18==6 | ch014_19==6 | ch014_20==6)
replace h`wv'kcntf = 5 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 5) | (inrange(sn005_2,10,13) & sn007_2 == 5) | (inrange(sn005_3,10,13) & sn007_3 == 5) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 5) | (inrange(sn005_5,10,13) & sn007_5 == 5) | (inrange(sn005_6,10,13) & sn007_6 == 5) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 5)) | ///
		(ch014_1==5 | ch014_2==5 | ch014_3==5 | ch014_4==5 | ch014_5==5 | ch014_6==5 | ch014_7==5 | ///
		ch014_8==5 | ch014_9==5 | ch014_10==5 | ch014_11==5 | ch014_12==5 | ch014_13==5 | ch014_14==5 | ch014_15==5 | ch014_16==5 | ch014_17==5 | ///
		ch014_18==5 | ch014_19==5 | ch014_20==5)
replace h`wv'kcntf = 4 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 4) | (inrange(sn005_2,10,13) & sn007_2 == 4) | (inrange(sn005_3,10,13) & sn007_3 == 4) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 4) | (inrange(sn005_5,10,13) & sn007_5 == 4) | (inrange(sn005_6,10,13) & sn007_6 == 4) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 4)) | ///
		(ch014_1==4 | ch014_2==4 | ch014_3==4 | ch014_4==4 | ch014_5==4 | ch014_6==4 | ch014_7==4 | ///
		ch014_8==4 | ch014_9==4 | ch014_10==4 | ch014_11==4 | ch014_12==4 | ch014_13==4 | ch014_14==4 | ch014_15==4 | ch014_16==4 | ch014_17==4 | ///
		ch014_18==4 | ch014_19==4 | ch014_20==4)
replace h`wv'kcntf = 3 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 3) | (inrange(sn005_2,10,13) & sn007_2 == 3) | (inrange(sn005_3,10,13) & sn007_3 == 3) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 3) | (inrange(sn005_5,10,13) & sn007_5 == 3) | (inrange(sn005_6,10,13) & sn007_6 == 3) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 3)) | ///
		(ch014_1==3 | ch014_2==3 | ch014_3==3 | ch014_4==3 | ch014_5==3 | ch014_6==3 | ch014_7==3 | ///
		ch014_8==3 | ch014_9==3 | ch014_10==3 | ch014_11==3 | ch014_12==3 | ch014_13==3 | ch014_14==3 | ch014_15==3 | ch014_16==3 | ch014_17==3 | ///
		ch014_18==3 | ch014_19==3 | ch014_20==3)
replace h`wv'kcntf = 2 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 2) | (inrange(sn005_2,10,13) & sn007_2 == 2) | (inrange(sn005_3,10,13) & sn007_3 == 2) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 2) | (inrange(sn005_5,10,13) & sn007_5 == 2) | (inrange(sn005_6,10,13) & sn007_6 == 2) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 2)) | ///
		(ch014_1==2 | ch014_2==2 | ch014_3==2 | ch014_4==2 | ch014_5==2 | ch014_6==2 | ch014_7==2 | ///
		ch014_8==2 | ch014_9==2 | ch014_10==2 | ch014_11==2 | ch014_12==2 | ch014_13==2 | ch014_14==2 | ch014_15==2 | ch014_16==2 | ch014_17==2 | ///
		ch014_18==2 | ch014_19==2 | ch014_20==2)
replace h`wv'kcntf = 1 if nchild != 0 & ((inrange(sn005_1,10,13) & sn007_1 == 1) | (inrange(sn005_2,10,13) & sn007_2 == 1) | (inrange(sn005_3,10,13) & sn007_3 == 1) | ///
		(inrange(sn005_4,10,13) & sn007_4 == 1) | (inrange(sn005_5,10,13) & sn007_5 == 1) | (inrange(sn005_6,10,13) & sn007_6 == 1) | ///
		(inrange(sn005_7,10,13) & sn007_7 == 1) | h`wv'coresd == 1) | ///
		(ch014_1==1 | ch014_2==1 | ch014_3==1 | ch014_4==1 | ch014_5==1 | ch014_6==1 | ch014_7==1 | ///
		ch014_8==1 | ch014_9==1 | ch014_10==1 | ch014_11==1 | ch014_12==1 | ch014_13==1 | ch014_14==1 | ch014_15==1 | ch014_16==1 | ch014_17==1 | ///
		ch014_18==1 | ch014_19==1 | ch014_20==1)
label variable h`wv'kcntf "h`wv'kcntf:w`wv' frequency contact with children in person/phone/mail"
label values h`wv'kcntf freqcont

bysort h`wv'coupid: replace h`wv'kcntf = h`wv'kcntf[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'kcntf = h`wv'kcntf[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

***Any Weekly Contact with Parents***
*wave 6 respondent any weekly contact with parents
gen r`wv'pcnt = .
missing_common dn032_1 dn032_2 dn030_1 dn030_2, result(r`wv'pcnt) wave(`wv')
replace r`wv'pcnt = .n if r`wv'livpar == 0
replace r`wv'pcnt = 0 if r`wv'livpar != 0 & (inrange(dn032_1,4,7) | inrange(dn032_2,4,7))
replace r`wv'pcnt = 1 if r`wv'livpar != 0 & (inrange(dn032_1,1,3) | inrange(dn032_2,1,3) | dn030_1 == 1 | dn030_2 == 1)
label variable r`wv'pcnt "r`wv'pcnt:w`wv' r any weekly contact with parents in person/phone/mail"
label values r`wv'pcnt contact

*wave 6 spouse any weekly contact with parents
gen s`wv'pcnt = .
spouse r`wv'pcnt, result(s`wv'pcnt) wave(`wv')
label variable s`wv'pcnt "s`wv'pcnt:w`wv' s any weekly contact with parents in person/phone/mail"
label values s`wv'pcnt contact

***Frequency of Contact with Parents***
*wave 6 respondent frequency contact with parents
gen r`wv'pcntf = .
missing_common dn032_1 dn032_2 dn030_1 dn030_2, result(r`wv'pcntf) wave(`wv')
replace r`wv'pcntf = .n if r`wv'livpar == 0
replace r`wv'pcntf = 7 if r`wv'livpar != 0 & (dn032_1 == 7 | dn032_2 == 7 | (inrange(sn005_1,2,7) & sn007_1 == 7) |  (inrange(sn005_2,2,7) & sn007_2 == 7) | ///
		(inrange(sn005_3,2,7) & sn007_3 == 7) | (inrange(sn005_4,2,7) & sn007_4 == 7) | (inrange(sn005_5,2,7) & sn007_5 == 7) | ///
		(inrange(sn005_6,2,7) & sn007_6 == 7) | (inrange(sn005_7,2,7) & sn007_7 == 7))
replace r`wv'pcntf = 6 if r`wv'livpar != 0 & (dn032_1 == 6 | dn032_2 == 6 | (inrange(sn005_1,2,7) & sn007_1 == 6) |  (inrange(sn005_2,2,7) & sn007_2 == 6) | ///
		(inrange(sn005_3,2,7) & sn007_3 == 6) | (inrange(sn005_4,2,7) & sn007_4 == 6) | (inrange(sn005_5,2,7) & sn007_5 == 6) | ///
		(inrange(sn005_6,2,7) & sn007_6 == 6) | (inrange(sn005_7,2,7) & sn007_7 == 6))
replace r`wv'pcntf = 5 if r`wv'livpar != 0 & (dn032_1 == 5 | dn032_2 == 5 | (inrange(sn005_1,2,7) & sn007_1 == 5) |  (inrange(sn005_2,2,7) & sn007_2 == 5) | ///
		(inrange(sn005_3,2,7) & sn007_3 == 5) | (inrange(sn005_4,2,7) & sn007_4 == 5) | (inrange(sn005_5,2,7) & sn007_5 == 5) | ///
		(inrange(sn005_6,2,7) & sn007_6 == 5) | (inrange(sn005_7,2,7) & sn007_7 == 5))
replace r`wv'pcntf = 4 if r`wv'livpar != 0 & (dn032_1 == 4 | dn032_2 == 4 | (inrange(sn005_1,2,7) & sn007_1 == 4) |  (inrange(sn005_2,2,7) & sn007_2 == 4) | ///
		(inrange(sn005_3,2,7) & sn007_3 == 4) | (inrange(sn005_4,2,7) & sn007_4 == 4) | (inrange(sn005_5,2,7) & sn007_5 == 4) | ///
		(inrange(sn005_6,2,7) & sn007_6 == 4) | (inrange(sn005_7,2,7) & sn007_7 == 4))
replace r`wv'pcntf = 3 if r`wv'livpar != 0 & (dn032_1 == 3 | dn032_2 == 3 | (inrange(sn005_1,2,7) & sn007_1 == 3) |  (inrange(sn005_2,2,7) & sn007_2 == 3) | ///
		(inrange(sn005_3,2,7) & sn007_3 == 3) | (inrange(sn005_4,2,7) & sn007_4 == 3) | (inrange(sn005_5,2,7) & sn007_5 == 3) | ///
		(inrange(sn005_6,2,7) & sn007_6 == 3) | (inrange(sn005_7,2,7) & sn007_7 == 3))
replace r`wv'pcntf = 2 if r`wv'livpar != 0 & (dn032_1 == 2 | dn032_2 == 2 | (inrange(sn005_1,2,7) & sn007_1 == 2) |  (inrange(sn005_2,2,7) & sn007_2 == 2) | ///
		(inrange(sn005_3,2,7) & sn007_3 == 2) | (inrange(sn005_4,2,7) & sn007_4 == 2) | (inrange(sn005_5,2,7) & sn007_5 == 2) | ///
		(inrange(sn005_6,2,7) & sn007_6 == 2) | (inrange(sn005_7,2,7) & sn007_7 == 2))
replace r`wv'pcntf = 1 if r`wv'livpar != 0 & (dn032_1 == 1 | dn032_2 == 1 | dn030_1 == 1 | dn030_2 == 1 | (inrange(sn005_1,2,7) & sn007_1 == 1) | ///
		(inrange(sn005_2,2,7) & sn007_2 == 1) | (inrange(sn005_3,2,7) & sn007_3 == 1) | (inrange(sn005_4,2,7) & sn007_4 == 1) | ///
		(inrange(sn005_5,2,7) & sn007_5 == 1) | (inrange(sn005_6,2,7) & sn007_6 == 1) | (inrange(sn005_7,2,7) & sn007_7 == 1))
label variable r`wv'pcntf "r`wv'pcntf:w`wv' r frequency contact with parents in person/phone/mail"
label values r`wv'pcntf freqcont

*wave 6 spouse frequency contact with parents
gen s`wv'pcntf = .
spouse r`wv'pcntf, result(s`wv'pcntf) wave(`wv')
label variable s`wv'pcntf "s`wv'pcntf:w`wv' s frequency contact with parents in person/phone/mail"
label values s`wv'pcntf freqcont

***Received Any Informal Care***
*wave 6 household received any informal care
gen hout = .
missing_common sp002_, result(hout) wave(`wv')
replace hout = 0 if sp002_ == 5
replace hout = 1 if sp002_ == 1
bysort h`wv'coupid: replace hout = hout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace hout = hout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rin = .
missing_common sp020_ hhsize ph048dno, result(rin) wave(`wv')
replace rin = 0 if sp020_ == 5  | hhsize == 1 | ph048dno == 1 | ph049dno==1
replace rin = 1 if sp020_ == 1

gen spin = .
spouse rin, result(spin) wave(`wv')

gen h`wv'raany = .
replace h`wv'raany = .b if mi(spin) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .m if (hout == .m | rin == .m | spin == .m | r`wv'mstat == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .m if (hout == .m | rin == .m | r`wv'mstat == .m) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = .r if (hout == .r | rin == .r | spin == .r | r`wv'mstat == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .r if (hout == .r | rin == .r | r`wv'mstat == .r) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = .d if (hout == .d | rin == .d | spin == .d | r`wv'mstat == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = .d if (hout == .d | rin == .d | r`wv'mstat == .d) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = 0 if (hout == 0 & rin == 0 & spin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = 0 if (hout == 0 & rin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'raany = 1 if (hout == 1 | rin == 1 | spin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'raany = 1 if (hout == 1 | rin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
label variable h`wv'raany "h`wv'raany:w`wv' received any informal care"
label values h`wv'raany carer

drop hout rin spin

***Received Informal Care from Children***
*wave 6 household received informal care from children
gen chout = .
missing_common sp002_ sp003_1 sp003_2 sp003_3 nchild, result(chout) wave(`wv')
replace chout = .k if nchild == 0
replace chout = .x if sp003_1==96 | sp003_2==96 | sp003_3==96
replace chout = 0 if nchild != 0 & (sp002_ == 5 | (sp002_ == 1 & ((inrange(sp003_1,1,9) | inrange(sp003_1,22,37)) | ///
		(inrange(sp003_2,1,9) | inrange(sp003_2,22,37)) | (inrange(sp003_3,1,9) | inrange(sp003_3,22,37)))))
replace chout = 1 if nchild != 0 & (sp002_ == 1 & (inrange(sp003_1,10,21) | inrange(sp003_2,10,21) | inrange(sp003_3,10,21)))
bysort h`wv'coupid: replace chout = chout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace chout = chout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen crin = .
missing_common sp020_ sp021d10 sp021d11 sp021d20 sp021d21 hhsize ph048dno nchild, result(crin) wave(`wv')
replace crin = .k if nchild == 0
replace crin = .x if sp021dno==1
replace crin = 0 if hhsize == 1 | ph048dno == 1 | ph049dno == 1 | (nchild != 0 & (sp020_ == 5 | (sp020_ == 1 & (sp021d10 != 1 | sp021d11 != 1 | sp021d20 != 1 | sp021d21 != 1))))
replace crin = 1 if nchild != 0 & sp020_ == 1 & (sp021d10 == 1 | sp021d11 == 1 | sp021d20 == 1 | sp021d21 == 1)

gen cspin = .
spouse crin, result(cspin) wave (`wv')

gen h`wv'rccare = .
replace h`wv'rccare = .b if mi(cspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .m if (chout == .m | crin == .m | cspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .m if (chout == .m | crin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .r if (chout == .r | crin == .r | cspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .r if (chout == .r | crin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .d if (chout == .d | crin == .d | cspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .d if (chout == .d | crin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .k if (chout == .k | crin == .k | cspin == .k) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .k if (chout == .k | crin == .k) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = .x if (chout == .x | crin == .x | cspin == .x) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = .x if (chout == .x | crin == .x) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = 0 if (chout == 0 & crin == 0 & cspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = 0 if (chout == 0 & crin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'rccare = 1 if (chout == 1 | crin == 1 | cspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rccare = 1 if (chout == 1 | crin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
label variable h`wv'rccare "h`wv'rccare:w`wv' received informal care from children"
label values h`wv'rccare carer

drop chout crin cspin

***Received Informal Care from Relatives***
*wave 6 household received informal care from relatives
gen rhout = .
missing_common sp002_ sp003_1 sp003_2 sp003_3, result(rhout) wave(`wv')
replace rhout = .x if sp003_1==96 | sp003_2==96 | sp003_3==96
replace rhout = 0 if sp002_ == 5 | (sp002_ == 1 & ((inrange(sp003_1,10,21) | inrange(sp003_1,29,37)) | ///
		(inrange(sp003_2,10,21) | inrange(sp003_2,29,37)) | (inrange(sp003_3,10,21) | inrange(sp003_3,29,37))))
replace rhout = 1 if sp002_ == 1 & (inrange(sp003_1,1,9) | inrange(sp003_1,22,28) | inrange(sp003_2,1,9) | ///
		inrange(sp003_2,22,28) | inrange(sp003_3,1,9) | inrange(sp003_3,22,28))
bysort h`wv'coupid: replace rhout = rhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace rhout = rhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rrin = .
missing_common sp020_ sp021d1 sp021d2 sp021d3 sp021d4 sp021d5 sp021d6 sp021d7 sp021d8 sp021d9 sp021d22 sp021d23 sp021d24 sp021d25 ///
		sp021d26 sp021d27 sp021d28 hhsize ph048dno, result(rrin) wave(`wv')
replace rrin = .x if sp021dno==1
replace rrin = 0 if hhsize == 1 | ph048dno == 1 | ph049dno == 1 | (sp020_ == 5 | (sp020_ == 1 & (sp021d1 != 1 | sp021d2 != 1 | sp021d3 != 1 | sp021d4 != 1 | ///
		sp021d5 != 1 | sp021d6 != 1 | sp021d7 != 1 | sp021d8 != 1 | sp021d9 != 1 | sp021d22 != 1 | sp021d23 != 1 | sp021d24 != 1 | ///
		sp021d25 != 1 | sp021d26 != 1 | sp021d27 != 1 | sp021d28 != 1)))
replace rrin = 1 if sp020_ == 1 & (sp021d1 == 1 | sp021d2 == 1 | sp021d3 == 1 | sp021d4 == 1 | ///
		sp021d5 == 1 | sp021d6 == 1 | sp021d7 == 1 | sp021d8 == 1 | sp021d9 == 1 | sp021d22 == 1 | sp021d23 == 1 | sp021d24 == 1 | ///
		sp021d25 == 1 | sp021d26 == 1 | sp021d27 == 1 | sp021d28 == 1)

gen rspin = .
spouse rrin, result(rspin) wave (`wv')

gen h`wv'rrcare = .
replace h`wv'rrcare = .b if mi(rspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .m if (rhout == .m | rrin == .m | rspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .m if (rhout == .m | rrin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = .r if (rhout == .r | rrin == .r | rspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .r if (rhout == .r | rrin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = .d if (rhout == .d | rrin == .d | rspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .d if (rhout == .d | rrin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = .x if (rhout == .x | rrin == .x | rspin == .x) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = .x if (rhout == .x | rrin == .x) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = 0 if (rhout == 0 & rrin == 0 & rspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = 0 if (rhout == 0 & rrin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'rrcare = 1 if (rhout == 1 | rrin == 1 | rspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rrcare = 1 if (rhout == 1 | rrin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
label variable h`wv'rrcare "h`wv'rrcare:w`wv' received informal care from relatives"
label values h`wv'rrcare carer

drop rhout rrin rspin

***Received Informal Care from non-relatives***
*wave 6 household received informal care from non-relatives
gen fhout = .
missing_common sp002_ sp003_1 sp003_2 sp003_3, result(fhout) wave(`wv')
replace fhout = .x if sp003_1==96 | sp003_2==96 | sp003_3==96
replace fhout = 0 if sp002_ == 5 | (sp002_ == 1 & (inrange(sp003_1,1,28) | inrange(sp003_2,1,28) | inrange(sp003_3,1,28)))
replace fhout = 1 if sp002_ == 1 & (inrange(sp003_1,29,37) | inrange(sp003_2,29,37) | inrange(sp003_3,29,37))
bysort h`wv'coupid: replace fhout = fhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace fhout = fhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen frin = .
missing_common sp020_ sp021d29 sp021d30 sp021d31 sp021d32 sp021d35 sp021d36 sp021d37 hhsize ph048dno, result(frin) wave(`wv')
replace frin = .x if sp021dno==1
replace frin = 0 if hhsize == 1 | ph048dno == 1 | ph049dno == 1 | (sp020_ == 5 | (sp020_ == 1 & (sp021d29 != 1 | sp021d30 != 1 | sp021d31 != 1 | sp021d32 != 1 | ///
		sp021d35 != 1 | sp021d36 != 1 | sp021d37 != 1)))
replace frin = 1 if sp020_ == 1 & (sp021d29 == 1 | sp021d30 == 1 | sp021d31 == 1 | sp021d32 == 1 | ///
		sp021d35 == 1 | sp021d36 == 1 | sp021d37 == 1)

gen fspin = .
spouse frin, result(fspin) wave (`wv')

gen h`wv'rfcare = .
replace h`wv'rfcare = .b if mi(fspin) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .m if (fhout == .m | frin == .m | fspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .m if (fhout == .m | frin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = .r if (fhout == .r | frin == .r | fspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .r if (fhout == .r | frin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = .d if (fhout == .d | frin == .d | fspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .d if (fhout == .d | frin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = .x if (fhout == .x | frin == .x | fspin == .x) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = .x if (fhout == .x | frin == .x) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = 0 if (fhout == 0 & frin == 0 & fspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = 0 if (fhout == 0 & frin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'rfcare = 1 if (fhout == 1 | frin == 1 | fspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'rfcare = 1 if (fhout == 1 | frin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
label variable h`wv'rfcare "h`wv'rfcare:w`wv' received informal care from non-relatives"
label values h`wv'rfcare carer

drop fhout frin fspin

***Gave Any Informal Care***
*wave 6 respondent gave any informal care
gen hout = .
missing_common sp008_, result(hout) wave(`wv')
replace hout = 0 if sp008_ == 5
replace hout = 1 if sp008_ == 1
bysort h`wv'coupid: replace hout = hout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace hout = hout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rin = .
missing_common sp018_ hhsize, result(rin) wave(`wv')
replace rin = 0 if sp018_ == 5 | hhsize == 1
replace rin = 1 if sp018_ == 1

gen spin = .
spouse rin, result(spin) wave(`wv')

gen h`wv'gcaany = .
replace h`wv'gcaany = .b if mi(spin) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .m if (hout == .m | rin == .m | spin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .m if (hout == .m | rin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = .r if (hout == .r | rin == .r | spin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .r if (hout == .r | rin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = .d if (hout == .d | rin == .d | spin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = .d if (hout == .d | rin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = 0 if (hout == 0 & rin == 0 & spin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = 0 if (hout == 0 & rin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
replace h`wv'gcaany = 1 if (hout == 1 | rin == 1 | spin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gcaany = 1 if (hout == 1 | rin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(spin,.u,.v))
label variable h`wv'gcaany "h`wv'gcaany:w`wv' gave any informal care"
label values h`wv'gcaany careg

drop hout rin spin

***Gave Informal Care to Children***
*wave 6 respondent gave informal care to children
gen chout = .
missing_common sp008_ sp009_1 sp009_2 sp009_3 nchild, result(chout) wave(`wv')
replace chout = .k if nchild == 0
replace chout = .x if sp009_1==96 | sp009_2==96 | sp009_3==96
replace chout = 0 if nchild != 0 & (sp008_ == 5 | (sp008_ == 1 & ((inrange(sp009_1,1,9) | ///
		inrange(sp009_1,22,37)) | (inrange(sp009_2,1,9) | inrange(sp009_2,22,37)) | ///
		(inrange(sp009_3,1,9) | inrange(sp009_3,22,37)))))
replace chout = 1 if nchild != 0 & (sp008_ == 1 & (inrange(sp009_1,10,21) | inrange(sp009_2,10,21) | inrange(sp009_3,10,21)))
bysort h`wv'coupid: replace chout = chout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace chout = chout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen crin = .
missing_common sp018_ sp019d10 sp019d11 sp019d20 sp019d21 hhsize ph048dno nchild, result(crin) wave(`wv')
replace crin = .k if nchild == 0
replace crin = .x if sp019dno==1
replace crin = 0 if hhsize == 1 | ph048dno == 1 | (nchild != 0 & (sp018_ == 5 | (sp018_ == 1 & (sp019d10 != 1 | sp019d11 != 1 |  ///
		sp019d20 != 1 | sp019d21 != 1))))
replace crin = 1 if nchild != 0 & sp018_ == 1 & (sp019d10 == 1 | sp019d11 == 1 | sp019d20 == 1 | sp019d21 == 1)

gen cspin = .
spouse crin, result(cspin) wave(`wv')

gen h`wv'gccare = .
replace h`wv'gccare = .b if mi(cspin) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .m if (chout == .m | crin == .m | cspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .m if (chout == .m | crin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .r if (chout == .r | crin == .r | cspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .r if (chout == .r | crin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .d if (chout == .d | crin == .d | cspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .d if (chout == .d | crin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .k if (chout == .k | crin == .k | cspin == .k) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .k if (chout == .k | crin == .k) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = .x if (chout == .x | crin == .x | cspin == .x) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = .x if (chout == .x | crin == .x) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = 0 if (chout == 0 & crin == 0 & cspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = 0 if (chout == 0 & crin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
replace h`wv'gccare = 1 if (chout == 1 | crin == 1 | cspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gccare = 1 if (chout == 1 | crin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(cspin,.u,.v))
label variable h`wv'gccare "h`wv'gccare:w`wv' gave informal care to children"
label values h`wv'gccare careg

drop chout crin cspin

***Gave Informal Care to Relatives***
*wave 6 respondent gave informal care to relatives
gen rhout = .
missing_common sp008_ sp009_1 sp009_2 sp009_3, result(rhout) wave(`wv')
replace rhout = .x if sp009_1==96 | sp009_2==96 | sp009_3==96
replace rhout = 0 if sp008_ == 5 | (sp008_ == 1 & (inrange(sp009_1,10,21) | inrange(sp009_1,29,37) | inrange(sp009_2,10,21) | ///
		inrange(sp009_2,29,37) | inrange(sp009_3,10,21) | inrange(sp009_3,29,37)))
replace rhout = 1 if sp008_ == 1 & (inrange(sp009_1,1,9) | inrange(sp009_1,22,28) | inrange(sp009_2,1,9) | inrange(sp009_2,22,28) | ///
		inrange(sp009_3,1,9) | inrange(sp009_3,22,28))
bysort h`wv'coupid: replace rhout = rhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace rhout = rhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen rrin = .
missing_common sp018_ sp019d1 sp019d2 sp019d3 sp019d4 sp019d5 sp019d6 sp019d7 sp019d8 sp019d9 ///
		sp019d23 sp019d24 sp019d25 sp019d26 sp019d27 sp019d28 hhsize ph048dno, result(rrin) wave(`wv')
replace rrin = .x if sp019dno==1
replace rrin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & (sp019d1 != 1 | sp019d2 != 1 | sp019d3 != 1 | ///
		sp019d4 != 1 | sp019d5 != 1 | sp019d6 != 1 | sp019d7 != 1 | sp019d8 != 1 | sp019d9 != 1 | sp019d23 != 1 | sp019d24 != 1 | ///
		sp019d25 != 1 | sp019d26 != 1 | sp019d27 != 1 | sp019d28 != 1)))
replace rrin = 1 if sp018_ == 1 & (sp019d1 == 1 | sp019d2 == 1 | sp019d3 == 1 | sp019d4 == 1 | sp019d5 == 1 | sp019d6 == 1 | sp019d7 == 1 | ///
		sp019d8 == 1 | sp019d9 == 1 | sp019d23 == 1 | sp019d24 == 1 | sp019d25 == 1 | sp019d26 == 1 | sp019d27 == 1 | sp019d28 == 1)

gen rspin = .
spouse rrin, result(rspin) wave(`wv')

gen h`wv'grcare = .
replace h`wv'grcare = .b if mi(rspin) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .m if (rhout == .m | rrin == .m | rspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .m if (rhout == .m | rrin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = .r if (rhout == .r | rrin == .r | rspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .r if (rhout == .r | rrin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = .d if (rhout == .d | rrin == .d | rspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .d if (rhout == .d | rrin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = .x if (rhout == .x | rrin == .x | rspin == .x) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = .x if (rhout == .x | rrin == .x) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = 0 if (rhout == 0 & rrin == 0 & rspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = 0 if (rhout == 0 & rrin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
replace h`wv'grcare = 1 if (rhout == 1 | rrin == 1 | rspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'grcare = 1 if (rhout == 1 | rrin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(rspin,.u,.v))
label variable h`wv'grcare "h`wv'grcare:w`wv' gave informal care to relatives"
label values h`wv'grcare careg

drop rhout rrin rspin

***Gave Informal Care to non-relatives***
*wave 6 respondent gave informal care to non-relatives
gen fhout = .
missing_common sp008_ sp009_1 sp009_2 sp009_3, result(fhout) wave(`wv')
replace fhout = .x if sp009_1==96 | sp009_2==96 | sp009_3==96
replace fhout = 0 if sp008_ == 5 | (sp008_ == 1 & (inrange(sp009_1,1,28) | inrange(sp009_2,1,28) | inrange(sp009_3,1,28)))
replace fhout = 1 if sp008_ == 1 & (inrange(sp009_1,29,37) | inrange(sp009_2,29,37) | inrange(sp009_3,29,37))
bysort h`wv'coupid: replace fhout = fhout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace fhout = fhout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen frin = .
missing_common sp018_ sp019d29 sp019d30 sp019d31 sp019d32 sp019d35 sp019d36 sp019d37 hhsize ph048dno, result(frin) wave(`wv')
replace frin = .x if sp019dno==1
replace frin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & (sp019d29 != 1 | sp019d30 != 1 | sp019d31 != 1 | ///
		sp019d32 != 1 | sp019d35 != 1 | sp019d36 != 1 | sp019d37 != 1)))
replace frin = 1 if sp018_ == 1 & (sp019d29 == 1 | sp019d30 == 1 | sp019d31 == 1 | sp019d32 == 1 | sp019d35 != 1 | sp019d36 != 1 | sp019d37 != 1)

gen fspin = .
spouse frin, result(fspin) wave(`wv')

gen h`wv'gfcare = .
replace h`wv'gfcare = .b if mi(fspin) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .m if (fhout == .m | frin == .m | fspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .m if (fhout == .m | frin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = .r if (fhout == .r | frin == .r | fspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .r if (fhout == .r | frin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = .d if (fhout == .d | frin == .d | fspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .d if (fhout == .d | frin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = .x if (fhout == .x | frin == .x | fspin == .x) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = .x if (fhout == .x | frin == .x) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = 0 if (fhout == 0 & frin == 0 & fspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = 0 if (fhout == 0 & frin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
replace h`wv'gfcare = 1 if (fhout == 1 | frin == 1 | fspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gfcare = 1 if (fhout == 1 | frin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(fspin,.u,.v))
label variable h`wv'gfcare "h`wv'gfcare:w`wv' gave informal care to non-relatives"
label values h`wv'gfcare careg

drop fhout frin fspin

***Gave Informal Care to Grandchildren***
*wave 6 respondent gave care to grandchildren
gen khout = .
missing_common sp014_ ngrchild, result(khout) wave(`wv')
replace khout = .g if ngrchild == 0
replace khout = 0 if ngrchild != 0 & sp014_ == 5
replace khout = 1 if ngrchild != 0 & sp014_ == 1
bysort h`wv'coupid: replace khout = khout[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'famr == 0 // copy values to spouse
bysort h`wv'coupid: replace khout = khout[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'famr == 0 // copy values to spouse

gen krin = .
missing_common sp018_ sp019d22 hhsize ph048dno, result(krin) wave(`wv')
replace krin = 0 if hhsize == 1 | ph048dno == 1 | (sp018_ == 5 | (sp018_ == 1 & sp019d22 != 1))
replace krin = 1 if sp018_ == 1 & sp019d22 == 1

gen kspin = .
spouse krin, result(kspin) wave(`wv')

gen h`wv'gkcare = .
replace h`wv'gkcare = .b if mi(kspin) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .m if (khout == .m | krin == .m | kspin == .m) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .m if (khout == .m | krin == .m) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .r if (khout == .r | krin == .r | kspin == .r) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .r if (khout == .r | krin == .r) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .d if (khout == .d | krin == .d | kspin == .d) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .d if (khout == .d | krin == .d) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = .g if (khout == .g | krin == .g | kspin == .g) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = .g if (khout == .g | krin == .g) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = 0 if (khout == 0 & krin == 0 & kspin == 0) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = 0 if (khout == 0 & krin == 0) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
replace h`wv'gkcare = 1 if (khout == 1 | krin == 1 | kspin == 1) & inlist(r`wv'mstat,1,3)
replace h`wv'gkcare = 1 if (khout == 1 | krin == 1) & (!inlist(r`wv'mstat,1,3) | inlist(kspin,.u,.v))
label variable h`wv'gkcare "h`wv'gkcare:w`wv' gave care to grandchildren"
label values h`wv'gkcare careg

drop khout krin kspin

***Any Transfer to Children/Grandchildren***
*wave 6 household any transfer to children/grandchildren
gen h`wv'tcany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'tcany) wave(`wv')
replace h`wv'tcany = .x if ft003_1==96 | ft003_2==96 | ft003_3==96
replace h`wv'tcany = 0 if ft002_ == 5 | (ft002_ == 1 & ((inrange(ft003_1,1,9) | inrange(ft003_1,23,37) | /// 
														inrange(ft003_2,1,9) | inrange(ft003_2,23,37) | ///
														inrange(ft003_3,1,9) | inrange(ft003_3,23,37))))
replace h`wv'tcany = 1 if ft002_ == 1 & (inrange(ft003_1,10,22) | inrange(ft003_2,10,22) | inrange(ft003_3,10,22))
label variable h`wv'tcany "h`wv'tcany:w`wv' any transfer to children/grandchildren"
label values h`wv'tcany trans

bysort h`wv'coupid: replace h`wv'tcany = h`wv'tcany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tcany = h`wv'tcany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers to Parents***
*wave 6 household any transfers to parents
gen h`wv'tpany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'tpany) wave(`wv')
replace h`wv'tpany = .x if ft003_1==96 | ft003_2==96 | ft003_3==96
replace h`wv'tpany = 0 if ft002_ == 5 | (ft002_ == 1 & (ft003_1 == 1 | inrange(ft003_1,8,37) | ///
														ft003_2 == 1 | inrange(ft003_2,8,37) | ///
														ft003_3 == 1 | inrange(ft003_3,8,37)))
replace h`wv'tpany = 1 if ft002_ == 1 & (inrange(ft003_1,2,7) | inrange(ft003_2,2,7) | inrange(ft003_3,2,7))
label variable h`wv'tpany "h`wv'tpany:w`wv' any transfer to parents"
label values h`wv'tpany trans

bysort h`wv'coupid: replace h`wv'tpany = h`wv'tpany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tpany = h`wv'tpany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers to Other Relatives***
*wave 6 household any transfers to other relatives
gen h`wv'toany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'toany) wave(`wv')
replace h`wv'toany = .x if ft003_1==96 | ft003_2==96 | ft003_3==96
replace h`wv'toany = 0 if ft002_ == 5 | (ft002_ == 1 & (inrange(ft003_1,2,7) | inrange(ft003_1,10,22) | inrange(ft003_1,29,37) | ///
														inrange(ft003_2,2,7) | inrange(ft003_2,10,22) | inrange(ft003_2,29,37) | ///
														inrange(ft003_3,2,7) | inrange(ft003_3,10,22) | inrange(ft003_3,29,37)))
replace h`wv'toany = 1 if ft002_ == 1 & (inlist(ft003_1,1,8,9) | inrange(ft003_1,23,28) | ///
										inlist(ft003_2,1,8,9) | inrange(ft003_2,23,28) | ///
										inlist(ft003_3,1,8,9) | inrange(ft003_3,23,28))
label variable h`wv'toany "h`wv'toany:w`wv' any transfer to other relatives"
label values h`wv'toany trans

bysort h`wv'coupid: replace h`wv'toany = h`wv'toany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'toany = h`wv'toany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers to Other Non-Relatives***
*wave 6 household any transfers to other non-relatives
gen h`wv'tfany = .
missing_common ft002_ ft003_1 ft003_2 ft003_3, result(h`wv'tfany) wave(`wv')
replace h`wv'tfany = .x if ft003_1==96 | ft003_2==96 | ft003_3==96
replace h`wv'tfany = 0 if ft002_ == 5 | (ft002_ == 1 & (inrange(ft003_1,1,28) | inrange(ft003_2,1,28) | inrange(ft003_3,1,28)))
replace h`wv'tfany = 1 if ft002_ == 1 & (inrange(ft003_1,29,37) | inrange(ft003_2,29,37) | inrange(ft003_3,29,37))
label variable h`wv'tfany "h`wv'tfany:w`wv' any transfer to other non-relatives"
label values h`wv'tfany trans

bysort h`wv'coupid: replace h`wv'tfany = h`wv'tfany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'tfany = h`wv'tfany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers from Children/Grandchildren***
*wave 6 household any transfers from children/grandchildren
gen h`wv'fcany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'fcany) wave(`wv')
replace h`wv'fcany = 0 if ft009_ == 5 | (ft009_ == 1 & (inrange(ft010_1,1,9) | inrange(ft010_1,23,96) | ///
														inrange(ft010_2,1,9) | inrange(ft010_2,23,96) | ///
														inrange(ft010_3,1,9) | inrange(ft010_3,23,96)))
replace h`wv'fcany = 1 if ft009_ == 1 & (inrange(ft010_1,10,22) | inrange(ft010_2,10,22) | inrange(ft010_3,10,22))
label variable h`wv'fcany "h`wv'fcany:w`wv' any transfers from children/grandchildren"
label values h`wv'fcany trans

bysort h`wv'coupid: replace h`wv'fcany = h`wv'fcany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fcany = h`wv'fcany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers from Parents***
*wave 6 household any transfers from parents
gen h`wv'fpany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'fpany) wave(`wv')
replace h`wv'fpany = 0 if ft009_ == 5 | (ft009_ == 1 & (ft010_1 == 1 | inrange(ft010_1,8,96) | /// 
														ft010_2 == 1 | inrange(ft010_2,8,96) | ///
		 												ft010_3 == 1 | inrange(ft010_3,8,96)))
replace h`wv'fpany = 1 if ft009_ == 1 & (inrange(ft010_1,2,7) | inrange(ft010_2,2,7) | inrange(ft010_3,2,7))
label variable h`wv'fpany "h`wv'fpany:w`wv' any transfers from parents"
label values h`wv'fpany trans

bysort h`wv'coupid: replace h`wv'fpany = h`wv'fpany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'fpany = h`wv'fpany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers from Other Relatives***
*wave 6 household any transfers from other relatives
gen h`wv'foany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'foany) wave(`wv')
replace h`wv'foany = 0 if ft009_ == 5 | (ft009_ == 1 & (inrange(ft010_1,2,7) | inrange(ft010_1,10,22) | inrange(ft010_1,29,96) | ///
	 													inrange(ft010_2,2,7) | inrange(ft010_2,10,22) | inrange(ft010_2,29,96) | ///
	 													inrange(ft010_3,2,7) | inrange(ft010_3,10,22) | inrange(ft010_3,29,96)))
replace h`wv'foany = 1 if ft009_ == 1 & (inlist(ft010_1,1,8,9) | inrange(ft010_1,23,28) | ///
										inlist(ft010_2,1,8,9) | inrange(ft010_2,23,28) | ///
										inlist(ft010_3,1,8,9) | inrange(ft010_3,23,28))
label variable h`wv'foany "h`wv'foany:w`wv' any transfers from other relatives"
label values h`wv'foany trans

bysort h`wv'coupid: replace h`wv'foany = h`wv'foany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'foany = h`wv'foany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Transfers from Other Non-Relatives***
*wave 6 household any transfers from other non-relatives
gen h`wv'ffany = .
missing_common ft009_ ft010_1 ft010_2 ft010_3, result(h`wv'ffany) wave(`wv')
replace h`wv'ffany = 0 if ft009_ == 5 | (ft009_ == 1 & (inrange(ft010_1,1,28) | inrange(ft010_2,1,28) |  inrange(ft010_3,1,28)))
replace h`wv'ffany = 1 if ft009_ == 1 & (inrange(ft010_1,29,96) | inrange(ft010_2,29,96) | inrange(ft010_3,29,96))
label variable h`wv'ffany "h`wv'ffany:w`wv' any transfers from other non-relatives"
label values h`wv'ffany trans

bysort h`wv'coupid: replace h`wv'ffany = h`wv'ffany[_n-1] if h`wv'coupid == h`wv'coupid[_n-1] & r`wv'finr == 0 // copy values to spouse
bysort h`wv'coupid: replace h`wv'ffany = h`wv'ffany[_n+1] if h`wv'coupid == h`wv'coupid[_n+1] & r`wv'finr == 0 // copy values to spouse

***Any Social Activities in Past Year***changed from previous waves because frequency activities no longer asked
*wave 6 respondent any yearly social activities
gen r`wv'socyr = .
missing_common ac035d1 ac035d4 ac035d5 ac035d7 ac035dno, result(r`wv'socyr) wave(`wv')
replace r`wv'socyr = .p if inlist(r`wv'proxy,1,2)
replace r`wv'socyr = .n if mn024_==2
replace r`wv'socyr = 0 if ac035dno == 1 | ac035d1 == 0 | ac035d4 == 0 | ac035d5 == 0 | ac035d7 == 0 
replace r`wv'socyr = 1 if ac035d1 == 1 | ac035d4 == 1 | ac035d5 == 1  | ac035d7 == 1
label variable r`wv'socyr "r`wv'socyr:w`wv' r any yearly social activities"
label values r`wv'socyr yrsoc

*wave 6 spouse any yearly social activites
gen s`wv'socyr = .
spouse r`wv'socyr, result(s`wv'socyr) wave(`wv')
label variable s`wv'socyr "s`wv'socyr:w`wv' s any yearly social activities"
label values s`wv'socyr yrsoc

/*
***Any Yearly Participation Religious Groups***change from previous waves
*wave 6 respondent any yearly religious groups
gen r`wv'relgyr = .
missing_common ac035d6 ac035dno, result(r`wv'relgyr) wave(`wv')
replace r`wv'relgyr = 0 if ac035dno == 1 | ac035d6 == 0
replace r`wv'relgyr = 1 if ac035d6 == 1
replace r`wv'relgyr = .s if inlist(country,11,12,13,14,16,18,20,28,31,34) | (country == 25 & inlist(language,25,27)) | (country==23 & language == 24)
label variable r`wv'relgyr "r`wv'relgyr:w`wv' r any yearly religious groups"
label values r`wv'relgyr yrsoc

*wave 6 spouse any yearly religious groups
gen s`wv'relgyr = .
spouse r`wv'relgyr, result(s`wv'relgyr) wave(`wv')
label variable s`wv'relgyr "s`wv'relgyr:w`wv' s any yearly religious groups"
label values s`wv'relgyr yrsoc
*/

***drop wave 6 cover screen (all) file raw variables and non-respondent observations
drop `family_w6_cv'

***drop wave 6 cover screen (respondent) file raw variables
drop `family_w6_cv_r'

***drop wave 6 Demographic file raw variables
drop `family_w6_dn'

***drop wave 6 Children file raw variables
drop `family_w6_ch'

***drop wave 6 imputation file raw variables***
drop `family_w6_imput'

***drop wave 6 sp file raw variables
drop `family_w6_sp'

***drop wave 6 financial transfer file raw variables
drop `family_w6_ft'

***drop wave 6 activities file raw variables
drop `family_w6_ac'

***drop wave 6 ph raw variables***
drop `family_w6_ph'

***drop wave 6 sn raw variables***
drop `family_w6_sn'

***drop wave 6 technical variables file raw variables***
drop `family_w6_techvar'

******************************************************************************************

*Whether working for pay	
label define work ///
   0 "0.Not working for pay"  ///
   1 "1.Working for pay" 

*Whether self-employed
label define slfemp ///
   0 "0.Not self-employed"  ///
   1 "1.Self-employed" ///
   .w ".w:not working"

*Labor force status
label define lbrf_s ///
	.o ".o:Other" ///
	.d ".d:DK" ///
  .r ".r:Refuse" ///
  .m ".m:missing" ///
   1 "1.employed or self-employed" ///
   3 "3.unemployed" ///
   5 "5.retired" ///S
   6 "6.permanently sick or disabled" ///
   8 "8.homemaker" 
 
*Whether job requires lots of physical effort
label define ja ///
   1 "1.Strongly Agree" ///
   2 "2.Agree" ///
   3 "3.Disagree" ///
   4 "4.Strongly Disagree" ///
   .w ".w:not working" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .j ".j:not asked" ///
   .m ".m:missing" ///
   .s ".s:Skipped"
   
***Wage imputation flag
label define wageimpflag ///
 -10 "-10.Employee or civil servant unknown" ///
  -9 "-9.Working for pay is unknown" ///
  -8 "-8.Self-employed" ///
  -7 "-7.Not working for pay" ///
  -1 "-1.Could not imptue:No donors" ///
   1 "1.reported value"  ///
   5 "5.Imputed value" 

***Wage flag
label define wageflag ///
  -10 "-10.Employee or civil servant unknown" ///
  -9 "-9.Working for pay is unknown" ///
  -8 "-8.Self-employed" ///
  -7 "-7.Not working for pay" ///
  -1 "-1.Could not imptue:No donors" ///
   1 "1.continuous value" ///
   2 "2.complete bracket"       ///
   3 "3.incomplete bracket"     ///
   5 "5.no value/bracket" 
   
***ISCO-88 Occupation Codes
label define isco88   ///
	 0 "0.Armed forces" ///
	 1 "1.Legislators, senior officials, managers" ///
	 2 "2.Professionals" ///
	 3 "3.Technicians and associate professionals" ///
	 4 "4.Clerks" ///
	 5 "5.Service workers and shop and market sales workers" ///
	 6 "6.Skilled agricultural and fishery workers" ///
	 7 "7.Craft and related trades workers" ///
	 8 "8.Plant and machine operators and assemblers" ///
	 9 "9.Elementary occupations" ///
	 .o ".o:Other" ///
	 .a ".a:Does not apply" ///
	 .d ".d:DK" ///
   .r ".r:Refuse" ///
   .i ".i:Invalid" ///
   .n ".n:No answer"  ///
   .e ".e:Not employed" ///
   .s ".s:getting education" ///
   .h ".h:housewife/man" ///
   .w ".w:not working"
	 
label define nace   ///
	1 "1.Agriculture, hunting, forestry, fishing" /// 01 02 05
	2 "2.Mining and quarrying" /// 10 11 12 13 14 
	3 "3.Manufacturing" /// 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	4 "4.Electricity, gas and water supply" /// 40 41 
	5 "5.Construction" /// 45 
	6 "6.Wholesale and retail trade; repair of motor vehicles, motorcycles and personal and household goods" /// 50 51 52
	7 "7.Hotels and restaurants" /// 55
	8 "8.Transport, storage and communication" /// 60 61 62 63 64
	9 "9.Financial intermediation" /// 65 66 67 
	10 "10.Real estate, renting and business activities" /// 70 71 72 73 74 
	11 "11.Public administration and defence; compulsory social security" /// 75 
	12 "12.Education" /// 80 
	13 "13.Health and social work" /// 85 
	14 "14.Other community, social and personal service activities" /// 90 91 92 93
	15 "15.Private households with employed persons " /// 95
	16 "16.Extra-territorial organizations and bodies " /// 99
	.o ".o:Other" ///
	.a ".a:Does not apply" ///
	.d ".d:DK" ///
  .r ".r:Refuse" ///
  .i ".i:Illegible" ///
  .n ".n:No answer" ///
  .m ".m:missing" ///
  .w ".w:not working"
	 
	    
******************************************************************************************



*set wave numer
local wv = 1

***merge with wave 1 Employment and Pension data***
local employ_w1_ep ep002_ ep003_ ep005_ ep007_ ep009_1 ep013_2 ep014_1 ep014_2 ///
				    ep038_1 ep027_ ep028_ ep010_1 ep050_ ep006_ ep012_1 ep013_1 ///
				    ep041e_1 ep041ub_1 ep041v1 ep041v2 ep041v3
merge 1:1 mergeid using "$wave_1_ep", keepusing(`employ_w1_ep') nogen

***merge with wave 1 Generated Variables (ISCO) data***
local employ_w1_gv_isco isco_1job nace_1job text_1job ind_1job
merge 1:1 mergeid using "$wave_1_gv_isco", keepusing(`employ_w1_gv_isco') nogen


***Currently working for pay***
*wave 1 respondent working for pay
gen r`wv'work=.
missing_common ep002_ ep005_, result(r`wv'work)
replace r`wv'work = 0 if ep002_ == 5 & ep002_ != 1
replace r`wv'work = 1 if ep005_ == 2 | ep002_ == 1
label variable r`wv'work "r`wv'work:w1 R working for pay"
label values r`wv'work work

*wave 1 spouse working for pay
gen s`wv'work=.
spouse r`wv'work, result(s`wv'work) wave(`wv')
label variable s`wv'work "s`wv'work:w1 S working for pay"
label values s`wv'work work

*wave 1 respondent works at second job
gen r`wv'work2=.
missing_common ep005_ ep007_, result(r`wv'work2)
replace r`wv'work2=0 if inlist(ep005_,1,3,4,5,97) | ep007_ ==5
replace r`wv'work2=1 if ep007_ == 1
label variable r`wv'work2 "r`wv'work2:w1 R Works at 2nd job"
label values r`wv'work2 work

*wave 1 spouse works at second job
gen s`wv'work2=.
spouse r`wv'work2, result(s`wv'work2) wave(`wv')
label variable s`wv'work2 "s`wv'work2:w1 S Works at 2nd job"
label values s`wv'work2 work

***Whether self-employed***
*wave 1 respondent whether self-employed
gen r`wv'slfemp=.
missing_common ep002_ ep003_ ep005_ ep009_1, result(r`wv'slfemp)
replace r`wv'slfemp = 0 if ep002_ == 5 | ep003_ == 5 | inlist(ep005_,1,3,4,5,97) | ///
												 inlist(ep009_1,1,2)
replace r`wv'slfemp = 1 if ep009_1 ==3
label variable r`wv'slfemp "r`wv'slfemp:w1 r Whether Self-Employed"
label values r`wv'slfemp slfemp

*wave 1 spouse whether self-employed
gen s`wv'slfemp=.
spouse r`wv'slfemp, result(s`wv'slfemp) wave(`wv')
label variable s`wv'slfemp "s`wv'slfemp:w1 s Whether Self-Employed"
label values s`wv'slfemp slfemp

***labor force status***
*wave 1 respondent labor force status
gen r`wv'lbrf_s=.
missing_common ep005_, result(r`wv'lbrf_s)
replace r`wv'lbrf_s = .o if ep005_== 97
replace r`wv'lbrf_s = 5 if ep005_ == 1
replace r`wv'lbrf_s = 1 if ep005_ == 2
replace r`wv'lbrf_s = 3 if ep005_ == 3
replace r`wv'lbrf_s = 6 if ep005_ == 4
replace r`wv'lbrf_s = 8 if ep005_ == 5
label variable r`wv'lbrf_s "r`wv'lbrf_s:w1 R labor force status"
label values r`wv'lbrf_s lbrf_s

*wave 1 spouse labor force status
gen s`wv'lbrf_s=.
spouse r`wv'lbrf_s, result(s`wv'lbrf_s) wave(`wv')
label variable s`wv'lbrf_s "s`wv'lbrf_s:w1 S labor force status"
label values s`wv'lbrf_s lbrf_s

***Hours of work per week at current job***
*wave 1 respondent Hours of work per week at main job
gen r`wv'jhours=.
missing_common ep013_1, result(r`wv'jhours)
replace r`wv'jhours = .w if r`wv'work==0
replace r`wv'jhours = ep013_1 if inrange(ep013_1,0,168)
label variable r`wv'jhours "r`wv'jhours:w1 r Hours worked/week main job"

*wave 1 spouse Hours of work per week at main job
gen s`wv'jhours=.
spouse r`wv'jhours, result(s`wv'jhours) wave(`wv')
label variable s`wv'jhours "s`wv'jhours:w1 s Hours worked/week main job"

*wave 1 respondent hours worked per week at 2nd job
gen r`wv'jhour2=.
missing_common ep013_2, result(r`wv'jhour2)
replace r`wv'jhour2 = .w if r`wv'work2==0
replace r`wv'jhour2 = ep013_2 if inrange(ep013_2,0,168)
label variable r`wv'jhour2 "r`wv'jhour2:w1 r Hours worked/week 2nd job"

*wave 1 spouse hours worked per week at 2nd job
gen s`wv'jhour2=.
spouse r`wv'jhour2, result(s`wv'jhour2) wave(`wv')
label variable s`wv'jhour2 "s`wv'jhour2:w1 s Hours worked/week 2nd job"

***Weeks worked per year***
*wave 1 respondent weeks worked per year
gen r`wv'jweeks_s=.
missing_common ep014_1, result(r`wv'jweeks_s)
replace r`wv'jweeks_s = .w if r`wv'work ==0
replace r`wv'jweeks_s = ep014_1*4.3333 if inrange(ep014_1,1,12)
label variable r`wv'jweeks_s "r`wv'jweeks_s:w1 r Weeks worked/year main job"

*wave 1 spouse weeks worked per year
gen s`wv'jweeks_s=.
spouse r`wv'jweeks_s, result(s`wv'jweeks_s) wave(`wv')
label variable s`wv'jweeks_s "s`wv'jweeks_s:w1 s Weeks worked/year main job"

***Weeks worked per year***
*wave 1 respondent weeks worked per year at 2nd job
gen r`wv'jweek2_s=.
missing_common ep014_2, result(r`wv'jweek2_s)
replace r`wv'jweek2_s = .w if r`wv'work ==0
replace r`wv'jweek2_s = ep014_2*4.3333 if inrange(ep014_2,1,12)
label variable r`wv'jweek2_s "r`wv'jweek2_s:w1 r Weeks worked/year 2nd job"

*wave 1 spouse weeks worked per year at 2nd job
gen s`wv'jweek2_s=.
spouse r`wv'jweek2_s, result(s`wv'jweek2_s) wave(`wv')
label variable s`wv'jweek2_s "s`wv'jweek2_s:w1 s Weeks worked/year 2nd job"

***Wage rate***
// impute frequency of payment for respondents who gave amount of payment
share_hotdeck ep038_1 if inlist(ep009_1,1,2), level(respondent) wave(`wv') exclude_values(-2,-1)

gen r`wv'freq = ep038_1_i

gen r`wv'ffreq = ep038_1_i_f
label variable r`wv'ffreq "r`wv'ffreq:w1 r Flag Wage Rate: Frequency"
label values r`wv'ffreq wageimpflag

drop ep038_1_i ep038_1_i_f

// impute amount of payment
gen r`wv'payment = .
gen r`wv'fpayment = .
foreach p in 1 2 3 4 5 6 {      
    di
    di as text "Payment Freqency = `p'"        	       
    share_hotdeck ep041e_1 if r`wv'freq == `p', level(respondent) wave(`wv') ///
               exclude_values(-9999992,-9999991,-1344301.6603169146,-137602872.98347473, -403398646.64548111, -6518049.3454777319, -19558282.177422166, -1663858518.3951263, -65595639.320499897, -3407496933.25,-19362682768.882324, -1748250.2389506113, -22037080.95854044,  -1089288.0693022262 ) /// 
               bv1_var(ep041v1) bv2_var(ep041v2) bv3_var(ep041v3) bounds_var(ep041ub_1)
    replace r`wv'payment = ep041e_1_i if !mi(ep041e_1_i)
    replace r`wv'fpayment =  ep041e_1_i_f if !mi(ep041e_1_i_f)
    drop ep041e_1_i ep041e_1_i_f
}
label variable r`wv'fpayment "r`wv'fpayment:w1 r Flag Wage Rate: Payment"
label values r`wv'fpayment wageflag
	              	
// impute number of hours worked per week
share_hotdeck ep012_1 if inlist(ep009_1,1,2), level(respondent) wave(`wv') exclude_values(-2,-1)

gen r`wv'hours = ep012_1_i
gen r`wv'fhours = ep012_1_i_f
label variable r`wv'fhours "r`wv'fhours:w1 r Flag Wage Rate: Hours"
label values r`wv'fhours wageimpflag

drop ep012_1_i ep012_1_i_f

*wave 1 respondent Wage Rate-Hrly
gen r`wv'wgihr_s =.
replace r`wv'wgihr_s = r`wv'payment/r`wv'hours if r`wv'freq==1
replace r`wv'wgihr_s = r`wv'payment/2/r`wv'hours if r`wv'freq==2
replace r`wv'wgihr_s = r`wv'payment/4/r`wv'hours if r`wv'freq==3
replace r`wv'wgihr_s = r`wv'payment/13/r`wv'hours if r`wv'freq==4
replace r`wv'wgihr_s = r`wv'payment/26/r`wv'hours if r`wv'freq==5
replace r`wv'wgihr_s = r`wv'payment/52/r`wv'hours if r`wv'freq==6
replace r`wv'wgihr_s = 0 if r`wv'hours==0 // assign a wage of 0 for respondents who reported working 0 hours

// impute wage rate for those respondents with remaining missing values
share_hotdeck r`wv'wgihr_s if inlist(ep009_1,1,2), level(respondent) wave(`wv')

replace r`wv'wgihr_s = r`wv'wgihr_s_i if mi(r`wv'wgihr_s) & !mi(r`wv'wgihr_s_i)
label variable r`wv'wgihr_s "r`wv'wgihr_s:w1 r Wage Rate-Hrly"

replace r`wv'ffreq = r`wv'wgihr_s_i_f if mi(r`wv'ffreq) & !mi(r`wv'wgihr_s_i_f)
replace r`wv'fpayment = r`wv'wgihr_s_i_f if mi(r`wv'fpayment) & !mi(r`wv'wgihr_s_i_f)
replace r`wv'fhours = r`wv'wgihr_s_i_f if mi(r`wv'fhours) & !mi(r`wv'wgihr_s_i_f)

drop r`wv'wgihr_s_i r`wv'wgihr_s_i_f

*wave 1 spouse Wage Rate-Hrly
gen s`wv'wgihr_s =.
spouse r`wv'wgihr_s, result(s`wv'wgihr_s) wave(`wv')
label variable s`wv'wgihr_s "s`wv'wgihr_s:w1 s Wage Rate-Hrly"

*wave 1 respondent Wage Rate-Wkly
gen r`wv'wgiwk_s =.
replace r`wv'wgiwk_s = r`wv'payment if r`wv'freq==1
replace r`wv'wgiwk_s = r`wv'payment/2 if r`wv'freq==2
replace r`wv'wgiwk_s = r`wv'payment/4 if r`wv'freq==3
replace r`wv'wgiwk_s = r`wv'payment/13 if r`wv'freq==4
replace r`wv'wgiwk_s = r`wv'payment/26 if r`wv'freq==5
replace r`wv'wgiwk_s = r`wv'payment/52 if r`wv'freq==6

// impute wage rate for those respondents with remaining missing values
share_hotdeck r`wv'wgiwk_s if inlist(ep009_1,1,2), level(respondent) wave(`wv')

replace r`wv'wgiwk_s = r`wv'wgiwk_s_i if mi(r`wv'wgiwk_s) & !mi(r`wv'wgiwk_s_i)
label variable r`wv'wgiwk_s "r`wv'wgiwk_s:w1 r Wage Rate-Wkly"

replace r`wv'ffreq = r`wv'wgiwk_s_i_f if mi(r`wv'ffreq) & !mi(r`wv'wgiwk_s_i_f)
replace r`wv'fpayment = r`wv'wgiwk_s_i_f if mi(r`wv'fpayment) & !mi(r`wv'wgiwk_s_i_f) 

drop r`wv'wgiwk_s_i r`wv'wgiwk_s_i_f

*wave 1 spouse Wage Rate-Wkly
gen s`wv'wgiwk_s =.
spouse r`wv'wgiwk_s, result(s`wv'wgiwk_s) wave(`wv')
label variable s`wv'wgiwk_s "s`wv'wgiwk_s:w1 s Wage Rate-Wkly"

*wave 1 respondent Flag Wage Rate-Hrly: Frequency
recode r`wv'ffreq (7=5)
replace r`wv'ffreq = -9 if mi(r`wv'work) & mi(r`wv'freq) & inw1==1
replace r`wv'ffreq = -7 if r`wv'work == 0 & mi(r`wv'freq)
replace r`wv'ffreq = -10 if inlist(ep009_1,.,-1,-2) & r`wv'work == 1 & mi(r`wv'freq)
replace r`wv'ffreq = -8 if ep009_1 == 3 & r`wv'work == 1 & mi(r`wv'freq)
label variable r`wv'ffreq "r`wv'ffreq:w1 r Flag Wage Rate: Frequency"
label values r`wv'ffreq wageimpflag

*wave 1 respondent Flag Wage Rate-Hrly: Payment
recode r`wv'fpayment (7=5)
replace r`wv'fpayment = -9 if mi(r`wv'work) & mi(r`wv'payment) & inw1==1
replace r`wv'fpayment = -7 if r`wv'work == 0 & mi(r`wv'payment)
replace r`wv'fpayment = -10 if inlist(ep009_1,.,-1,-2) & r`wv'work == 1 & mi(r`wv'payment)
replace r`wv'fpayment = -8 if ep009_1 == 3 & r`wv'work == 1 & mi(r`wv'payment)
label variable r`wv'fpayment "r`wv'fpayment:w1 r Flag Wage Rate: Payment"
label values r`wv'fpayment wageflag

*wave 1 respondent Flag Wage Rate-Hrly: Hours
recode r`wv'fhours (7=5)
replace r`wv'fhours = -9 if mi(r`wv'work) & mi(r`wv'hours) & inw1==1
replace r`wv'fhours = -7 if r`wv'work == 0 & mi(r`wv'hours)
replace r`wv'fhours = -10 if inlist(ep009_1,.,-1,-2) & r`wv'work == 1 & mi(r`wv'hours)
replace r`wv'fhours = -8 if ep009_1 == 3 & r`wv'work == 1 & mi(r`wv'hours)
label variable r`wv'fhours "r`wv'fhours:w1 r Flag Wage Rate: Hours"
label values r`wv'fhours wageimpflag

*wave 1 spouse Flag Wage Rate-Hrly: Frequency
gen s`wv'ffreq=.
spouse r`wv'ffreq, result(s`wv'ffreq) wave(`wv')
label variable s`wv'ffreq "s`wv'ffreq:w1 s Flag Wage Rate: Frequency"
label values s`wv'ffreq wageimpflag

*wave 1 spouse Flag Wage Rate-Hrly: Payment
gen s`wv'fpayment=.
spouse r`wv'fpayment, result(s`wv'fpayment) wave(`wv')
label variable s`wv'fpayment "s`wv'fpayment:w1 s Flag Wage Rate: Payment"
label values s`wv'fpayment wageflag

*wave 1 spouse Flag Wage Rate-Hrly: Hours
gen s`wv'fhours=.
spouse r`wv'fhours, result(s`wv'fhours) wave(`wv')
label variable s`wv'fhours "s`wv'fhours:w1 s Flag Wage Rate: Hours"
label values s`wv'fhours wageimpflag

drop r`wv'payment r`wv'freq r`wv'hours

***Current job requires lots of physical effort***
*wave 1 respondent whether job requires physical effort
gen r`wv'jphysa=.
missing_common ep027_, result(r`wv'jphysa)
replace r`wv'jphysa = .w if r`wv'work ==0
replace r`wv'jphysa =1 if ep027_ == 1
replace r`wv'jphysa =2 if ep027_ == 2
replace r`wv'jphysa =3 if ep027_ == 3
replace r`wv'jphysa =4 if ep027_ == 4
label variable r`wv'jphysa "r`wv'jphysa:w1 r Cur job req lots phys effort"
label values r`wv'jphysa ja

*wave 1 spouse whether job requires physical effort
gen s`wv'jphysa=.
spouse r`wv'jphysa, result(s`wv'jphysa) wave(`wv')
label variable s`wv'jphysa "s`wv'jphysa:w1 s Cur job req lots phys effort"
label values s`wv'jphysa ja

***Under time pressure due to workload***
*wave 1 respondent whether under time pressure due to workload 
gen r`wv'jpress=.
missing_common ep028_, result(r`wv'jpress)
replace r`wv'jpress = .w if r`wv'work ==0
replace r`wv'jpress =1 if ep028_ == 1
replace r`wv'jpress =2 if ep028_ == 2
replace r`wv'jpress =3 if ep028_ == 3
replace r`wv'jpress =4 if ep028_ == 4
label variable r`wv'jpress "r`wv'jpress:w1 r Under pressure due to workload"

*wave 1 spouse whether under time pressure due to workload
gen s`wv'jpress=.
spouse r`wv'jpress, result(s`wv'jpress) wave(`wv')
label variable s`wv'jpress "s`wv'jpress:w1 s Under pressure due to workload"
label values r`wv'jpress ja
label values s`wv'jpress ja

***ISCO Occupation code for current job***
*wave 1 respondent ISCO code for current job
gen isco_1job_ = substr(isco_1job,1,1)
destring isco_1job_, generate(r`wv'jisco)
replace r`wv'jisco = .m if missing(isco_1job) & inw`wv' == 1
replace r`wv'jisco = .a if isco_1job == "0000"
replace r`wv'jisco = .e if isco_1job == "0003"
replace r`wv'jisco = .o if isco_1job == "0004" | isco_1job == "0007"
replace r`wv'jisco = .s if isco_1job == "0005"
replace r`wv'jisco = .h if isco_1job == "0006" | isco_1job == "0060"
replace r`wv'jisco = .d if isco_1job == "0008"
replace r`wv'jisco = .n if isco_1job == "0009"
replace r`wv'jisco = .i if isco_1job == "9999"
replace r`wv'jisco = .w if r`wv'work==0
label variable r`wv'jisco "r`wv'jisco:w1 r ISCO code for current job"
label values r`wv'jisco isco88
drop isco_1job_

*wave 1 spouse ISCO code for current job
gen s`wv'jisco=.
spouse r`wv'jisco, result(s`wv'jisco) wave(`wv')
label variable s`wv'jisco "s`wv'jisco:w1 s ISCO code for current job"
label values s`wv'jisco isco88

***NACE Industry code for current job***
*wave 1 respondent NACE code for current job
gen nace_1job_ = substr(nace_1job,1,3)	
destring nace_1job_, generate(r`wv'jnace)
replace r`wv'jnace = .m if missing(nace_1job) & inw`wv' == 1
replace r`wv'jnace = 1 if inrange(r`wv'jnace,1,5)
replace r`wv'jnace = 2 if inrange(r`wv'jnace,10,14)
replace r`wv'jnace = 3 if inrange(r`wv'jnace,15,37)
replace r`wv'jnace = 4 if inrange(r`wv'jnace,40,41)
replace r`wv'jnace = 5 if r`wv'jnace == 45
replace r`wv'jnace = 6 if inrange(r`wv'jnace,50,52)
replace r`wv'jnace = 7 if r`wv'jnace == 55
replace r`wv'jnace = 8 if inrange(r`wv'jnace,60,64)
replace r`wv'jnace = 9 if inrange(r`wv'jnace,65,67)
replace r`wv'jnace = 10 if inrange(r`wv'jnace,70,74)
replace r`wv'jnace = 11 if r`wv'jnace == 75
replace r`wv'jnace = 12 if r`wv'jnace == 80
replace r`wv'jnace = 13 if r`wv'jnace == 85
replace r`wv'jnace = 14 if inrange(r`wv'jnace,90,93)
replace r`wv'jnace = 15 if r`wv'jnace == 95
replace r`wv'jnace = 16 if r`wv'jnace == 99
replace r`wv'jnace = .o if inlist(r`wv'jnace,86,87,88,98)
replace r`wv'jnace = .a if nace_1job == "0000"
replace r`wv'jnace = .n if nace_1job == "0004" | nace_1job == "0009"
replace r`wv'jnace = .i if r`wv'jnace == 76
replace r`wv'jnace = .r if r`wv'jnace == 77
replace r`wv'jnace = .d if r`wv'jnace == 78
replace r`wv'jnace = .n if r`wv'jnace == 79
replace r`wv'jnace = .w if r`wv'work==0
label variable r`wv'jnace "r`wv'jnace:w1 r NACE code for current job"
label values r`wv'jnace nace
drop nace_1job_

*wave 1 spouse NACE code for current job
gen s`wv'jnace=.
spouse r`wv'jnace, result(s`wv'jnace) wave(`wv')
label variable s`wv'jnace "s`wv'jnace:w1 s NACE code for current job"
label values s`wv'jnace nace

***Years of tenure on current job***
*wave 1 respondent years of tenure on current job
gen r`wv'jcten=.
missing_common ep010_1, result(r`wv'jcten)
replace r`wv'jcten = .w if r`wv'work ==0
replace r`wv'jcten = r`wv'iwy-ep010_1 ///
	if inrange(r`wv'iwy,2000,2010) & inrange(ep010_1,1900,2010)
replace r`wv'jcten = .n if r`wv'jcten<0
label variable r`wv'jcten "r`wv'jcten:w1 r Current Job Tenure"

*wave 1 spouse years of tenure on current job
gen s`wv'jcten=.
spouse r`wv'jcten, result(s`wv'jcten) wave(`wv')
label variable s`wv'jcten "s`wv'jcten:w1 s Current Job Tenure"

***year last job ended***
*wave 1 respondent year last job ended
gen r`wv'jlasty=.
missing_h r`wv'work, result(r`wv'jlasty)
missing_common ep050_, result(r`wv'jlasty)
replace r`wv'jlasty = .n if ep006_ ==5
replace r`wv'jlasty = ep050_ if inrange(ep050_,1900,2050)
replace r`wv'jlasty = .w if r`wv'work ==1
replace r`wv'jlasty = .i if r`wv'jlasty < rabyear & inrange(r`wv'jlasty,1900,2050) & inrange(rabyear,1890,2010)
label variable r`wv'jlasty "r`wv'jlasty:w1 r Year Last Worked/not workng"

*wave 1 spouse year last job ended
gen s`wv'jlasty=.
spouse r`wv'jlasty, result(s`wv'jlasty) wave(`wv')
label variable s`wv'jlasty "s`wv'jlasty:w1 s Year Last Worked/not workng"


***drop wave 1 Employment and Pension file raw variables
drop `employ_w1_ep'

***drop wave 1 Generated Variables (ISCO) file raw variables
drop `employ_w1_gv_isco'

******************************************************************************************

	
	

*set wave number
local wv = 2

***merge with cover screen tracker (respondent) data***
local employ_all_cv_r firstwave
merge 1:1 mergeid using "$tracker", keepusing(`employ_all_cv_r') nogen

***merge with wave 2 Employment and Pension data***
local employ_w2_ep ep002_ ep005_ ep007_ ep009_ ep014_ ep322_  ///
				    ep038_  ep027_ ep028_ ep050_ ep006_ ep013_ ep321_  ///
				    ep010_ ep141dno ep012_ ///
				    ep125_ ep141d? ep141dno ep016_ ep018_ ///
				    ep041e ep041ub ep041v1 ep041v2 ep041v3
merge 1:1 mergeid using "$wave_2_ep", keepusing(`employ_w2_ep') nogen


***Currently working for pay***
*wave 2 respondent working for pay
gen r2work=.
missing_common ep002_ ep005_,result(r2work)
replace r2work = 0 if ep002_ == 5 & ep002_ != 1
replace r2work = 1 if ep005_ == 2 | ep002_ == 1
label variable r2work "r2work:w2 R working for pay"
label values r2work work

*wave 2 spouse working for pay
gen s2work=.
spouse r2work, result(s2work) wave(2)
label variable s2work "s2work:w2 S working for pay"
label values s2work work

*wave 2 respondent works at second job
gen r2work2=.
missing_common ep005_ ep007_, result(r2work2)
replace r2work2=0 if inlist(ep005_,1,3,4,5,97) | ep007_ == 5
replace r2work2=1 if ep007_ == 1
label variable r2work2 "r2work2:w2 R Works at 2nd job"
label values r2work2 work

*wave 2 spouse works at second job
gen s2work2=.
spouse r2work2, result(s2work2) wave(2)
label variable s2work2 "s2work2:w2 S Works at 2nd job"
label values s2work2 work

***Whether self-employed***
*wave 2 respondent whether self-employed
gen r2slfemp=.
missing_common ep002_ ep005_ ep009_, result(r2slfemp)
replace r2slfemp = 0 if ep002_ == 5 | inlist(ep005_,1,3,4,5,97) | ///
												inlist(ep009_,1,2)
replace r2slfemp = 1 if ep009_ ==3
label variable r2slfemp "r2slfemp:w2 r Whether Self-Employed"

*wave 2 spouse whether self-employed
gen s2slfemp=.
spouse r2slfemp, result(s2slfemp) wave(2)
label variable s2slfemp "s2slfemp:w2 s Whether Self-Employed"

*value labels
label values r2slfemp slfemp
label values s2slfemp slfemp

***labor force status***
*wave 2 respondent labor force status
gen r2lbrf_s=.
missing_common ep005_, result(r2lbrf_s)
replace r2lbrf_s = .o if ep005_== 97
replace r2lbrf_s = 5 if ep005_ == 1
replace r2lbrf_s = 1 if ep005_ == 2
replace r2lbrf_s = 3 if ep005_ == 3
replace r2lbrf_s = 6 if ep005_ == 4
replace r2lbrf_s = 8 if ep005_ == 5
label variable r2lbrf_s "r2lbrf_s:w2 R labor force status"

*wave 2 spouse labor force status
gen s2lbrf_s=.
spouse r2lbrf_s, result(s2lbrf_s) wave(2)
label variable s2lbrf_s "s2lbrf_s:w2 S labor force status"

*value labels
label values r2lbrf_s lbrf_s
label values s2lbrf_s lbrf_s

***Hours of work per week at current job***
*wave 2 respondent Hours of work per week at main job
gen r2jhours=.
missing_common ep013_, result(r2jhours)
replace r2jhours = .w if r2work==0
replace r2jhours = ep013_ if inrange(ep013_,0,168)
label variable r2jhours "r2jhours:w2 r Hours worked/week main job"

*wave 2 spouse Hours of work per week at main job
gen s2jhours=.
spouse r2jhours, result(s2jhours) wave(2)
label variable s2jhours "s2jhours:w2 s Hours worked/week main job"

*wave 2 respondent hours worked per week at 2nd job
gen r2jhour2=.
missing_common ep321_, result(r2jhour2)
replace r2jhour2 = .w if r2work2==0
replace r2jhour2 = ep321_ if inrange(ep321_,0,168)
label variable r2jhour2 "r2jhour2:w2 r Hours worked/week 2nd job"

*wave 2 spouse hours worked per week at 2nd job
gen s2jhour2=.
spouse r2jhour2, result(s2jhour2) wave(2)
label variable s2jhour2 "s2jhour2:w2 s Hours worked/week 2nd job"

***Weeks worked per year***
*wave 2 respondent weeks worked per year
gen r2jweeks_s=.
missing_common ep014_, result(r2jweeks_s)
replace r2jweeks_s = .w if r2work ==0
replace r2jweeks_s = ep014_*4.3333 if inrange(ep014_,1,12)
label variable r2jweeks_s "r2jweeks_s:w2 r Weeks worked/year main job"

*wave 2 spouse weeks worked per year
gen s2jweeks_s=.
spouse r2jweeks_s, result(s2jweeks_s) wave(2)
label variable s2jweeks_s "s2jweeks_s:w2 s Weeks worked/year main job"

***Weeks worked per year***
*wave 2 respondent weeks worked per year at 2nd job
gen r2jweek2_s=.
missing_common ep322_, result(r2jweek2_s)
replace r2jweek2_s = .w if r2work ==0
replace r2jweek2_s = ep322_*4.3333 if inrange(ep322_,1,12)
label variable r2jweek2_s "r2jweek2_s:w2 r Weeks worked/year 2nd job"

*wave 2 spouse weeks worked per year at 2nd job
gen s2jweek2_s=.
spouse r2jweek2_s, result(s2jweek2_s) wave(2)
label variable s2jweek2_s "s2jweek2_s:w2 s Weeks worked/year 2nd job"

***Wage rate***
// impute frequency of payment for respondents who gave amount of payment
share_hotdeck ep038_ if inlist(ep009,1,2), level(respondent) wave(`wv') exclude_values(-2,-1)

gen r`wv'freq = ep038__i

gen r`wv'ffreq = ep038__i_f
label variable r`wv'ffreq "r`wv'ffreq:w`wv' r Flag Wage Rate: Frequency"
label values r`wv'ffreq wageimpflag

drop ep038__i ep038__i_f

// impute amount of payment
gen r`wv'payment = .
gen r`wv'fpayment = .
foreach p in 1 2 3 4 5 6 {      
    di
    di as text "Payment Freqency = `p'"        	       
    share_hotdeck ep041e if r`wv'freq == `p', level(respondent) wave(`wv') ///
               exclude_values(-9999992,-9999991,-355492.04757699248, -6169025.713945074, -1342280.7055060037, -1926780.5182560182, -2599425.8649437726, -1085775.3483801256) /// 
               bv1_var(ep041v1) bv2_var(ep041v2) bv3_var(ep041v3) bounds_var(ep041ub)
    replace r`wv'payment = ep041e_i if !mi(ep041e_i)
    replace r`wv'fpayment =  ep041e_i_f if !mi(ep041e_i_f)
    drop ep041e_i ep041e_i_f
}
label variable r`wv'fpayment "r`wv'fpayment:w`wv' r Flag Wage Rate: Payment"
label values r`wv'fpayment wageflag
	              	
// impute number of hours worked per week
share_hotdeck ep012_ if inlist(ep009,1,2), level(respondent) wave(`wv') exclude_values(-2,-1)

gen r`wv'hours = ep012__i
gen r`wv'fhours = ep012__i_f
label variable r`wv'fhours "r`wv'fhours:w`wv' r Flag Wage Rate: Hours"
label values r`wv'fhours wageimpflag

drop ep012__i ep012__i_f

*wave 2 respondent Wage Rate-Hrly
gen r`wv'wgihr_s =.
replace r`wv'wgihr_s = r`wv'payment/r`wv'hours if r`wv'freq==1
replace r`wv'wgihr_s = r`wv'payment/2/r`wv'hours if r`wv'freq==2
replace r`wv'wgihr_s = r`wv'payment/4/r`wv'hours if r`wv'freq==3
replace r`wv'wgihr_s = r`wv'payment/13/r`wv'hours if r`wv'freq==4
replace r`wv'wgihr_s = r`wv'payment/26/r`wv'hours if r`wv'freq==5
replace r`wv'wgihr_s = r`wv'payment/52/r`wv'hours if r`wv'freq==6
replace r`wv'wgihr_s = 0 if r`wv'hours==0 // assign a wage of 0 for respondents who reported working 0 hours

// impute wage rate for those respondents with remaining missing values
share_hotdeck r`wv'wgihr_s if inlist(ep009,1,2), level(respondent) wave(`wv')

replace r`wv'wgihr_s = r`wv'wgihr_s_i if mi(r`wv'wgihr_s) & !mi(r`wv'wgihr_s_i)
label variable r`wv'wgihr_s "r`wv'wgihr_s:w`wv' r Wage Rate-Hrly"

replace r`wv'ffreq = r`wv'wgihr_s_i_f if mi(r`wv'ffreq) & !mi(r`wv'wgihr_s_i_f)
replace r`wv'fpayment = r`wv'wgihr_s_i_f if mi(r`wv'fpayment) & !mi(r`wv'wgihr_s_i_f)
replace r`wv'fhours = r`wv'wgihr_s_i_f if mi(r`wv'fhours) & !mi(r`wv'wgihr_s_i_f)

drop r`wv'wgihr_s_i r`wv'wgihr_s_i_f

*wave 2 spouse Wage Rate-Hrly
gen s`wv'wgihr_s =.
spouse r`wv'wgihr_s, result(s`wv'wgihr_s) wave(`wv')
label variable s`wv'wgihr_s "s`wv'wgihr_s:w`wv' s Wage Rate-Hrly"

*wave 2 respondent Wage Rate-Wkly
gen r`wv'wgiwk_s =.
replace r`wv'wgiwk_s = r`wv'payment if r`wv'freq==1
replace r`wv'wgiwk_s = r`wv'payment/2 if r`wv'freq==2
replace r`wv'wgiwk_s = r`wv'payment/4 if r`wv'freq==3
replace r`wv'wgiwk_s = r`wv'payment/13 if r`wv'freq==4
replace r`wv'wgiwk_s = r`wv'payment/26 if r`wv'freq==5
replace r`wv'wgiwk_s = r`wv'payment/52 if r`wv'freq==6

// impute wage rate for those respondents with remaining missing values
share_hotdeck r`wv'wgiwk_s if inlist(ep009,1,2), level(respondent) wave(`wv')

replace r`wv'wgiwk_s = r`wv'wgiwk_s_i if mi(r`wv'wgiwk_s) & !mi(r`wv'wgiwk_s_i)
label variable r`wv'wgiwk_s "r`wv'wgiwk_s:w`wv' r Wage Rate-Wkly"

replace r`wv'ffreq = r`wv'wgiwk_s_i_f if mi(r`wv'ffreq) & !mi(r`wv'wgiwk_s_i_f)
replace r`wv'fpayment = r`wv'wgiwk_s_i_f if mi(r`wv'fpayment) & !mi(r`wv'wgiwk_s_i_f) 

drop r`wv'wgiwk_s_i r`wv'wgiwk_s_i_f

*wave 2 spouse Wage Rate-Wkly
gen s`wv'wgiwk_s =.
spouse r`wv'wgiwk_s, result(s`wv'wgiwk_s) wave(`wv')
label variable s`wv'wgiwk_s "s`wv'wgiwk_s:w`wv' s Wage Rate-Wkly"

*wave 2 respondent Flag Wage Rate-Hrly: Frequency
recode r`wv'ffreq (7=5)
replace r`wv'ffreq = -9 if mi(r`wv'work) & mi(r`wv'freq) & inw`wv'==1
replace r`wv'ffreq = -7 if r`wv'work == 0 & mi(r`wv'freq)
replace r`wv'ffreq = -10 if inlist(ep009,.,-1,-2) & r`wv'work == 1 & mi(r`wv'freq)
replace r`wv'ffreq = -8 if ep009 == 3 & r`wv'work == 1 & mi(r`wv'freq)
label variable r`wv'ffreq "r`wv'ffreq:w`wv' r Flag Wage Rate: Frequency"
label values r`wv'ffreq wageimpflag

*wave 2 respondent Flag Wage Rate-Hrly: Payment
recode r`wv'fpayment (7=5)
replace r`wv'fpayment = -9 if mi(r`wv'work) & mi(r`wv'payment) & inw`wv'==1
replace r`wv'fpayment = -7 if r`wv'work == 0 & mi(r`wv'payment)
replace r`wv'fpayment = -10 if inlist(ep009,.,-1,-2) & r`wv'work == 1 & mi(r`wv'payment)
replace r`wv'fpayment = -8 if ep009 == 3 & r`wv'work == 1 & mi(r`wv'payment)
label variable r`wv'fpayment "r`wv'fpayment:w`wv' r Flag Wage Rate: Payment"
label values r`wv'fpayment wageflag

*wave 2 respondent Flag Wage Rate-Hrly: Hours
recode r`wv'fhours (7=5)
replace r`wv'fhours = -9 if mi(r`wv'work) & mi(r`wv'hours) & inw`wv'==1
replace r`wv'fhours = -7 if r`wv'work == 0 & mi(r`wv'hours)
replace r`wv'fhours = -10 if inlist(ep009,.,-1,-2) & r`wv'work == 1 & mi(r`wv'hours)
replace r`wv'fhours = -8 if ep009 == 3 & r`wv'work == 1 & mi(r`wv'hours)
label variable r`wv'fhours "r`wv'fhours:w`wv' r Flag Wage Rate: Hours"
label values r`wv'fhours wageimpflag

*wave 2 spouse Flag Wage Rate-Hrly: Frequency
gen s`wv'ffreq=.
spouse r`wv'ffreq, result(s`wv'ffreq) wave(`wv')
label variable s`wv'ffreq "s`wv'ffreq:w`wv' s Flag Wage Rate: Frequency"
label values s`wv'ffreq wageimpflag

*wave 2 spouse Flag Wage Rate-Hrly: Payment
gen s`wv'fpayment=.
spouse r`wv'fpayment, result(s`wv'fpayment) wave(`wv')
label variable s`wv'fpayment "s`wv'fpayment:w`wv' s Flag Wage Rate: Payment"
label values s`wv'fpayment wageflag

*wave 2 spouse Flag Wage Rate-Hrly: Hours
gen s`wv'fhours=.
spouse r`wv'fhours, result(s`wv'fhours) wave(`wv')
label variable s`wv'fhours "s`wv'fhours:w`wv' s Flag Wage Rate: Hours"
label values s`wv'fhours wageimpflag

drop r`wv'payment r`wv'freq r`wv'hours
***Current job requires lots of physical effort***
*wave 2 respondent whether job requires physical effort
gen r2jphysa=.
missing_common ep027_, result(r2jphysa)
replace r2jphysa = .w if r2work ==0
replace r2jphysa =1 if ep027_ == 1
replace r2jphysa =2 if ep027_ == 2
replace r2jphysa =3 if ep027_ == 3
replace r2jphysa =4 if ep027_ == 4
label variable r2jphysa "r2jphysa:w2 r Cur job req lots phys effort"
label values r2jphysa ja

*wave 2 spouse whether job requires physical effort
gen s2jphysa=.
spouse r2jphysa, result(s2jphysa) wave(2)
label variable s2jphysa "s2jphysa:w2 s Cur job req lots phys effort"
label values s2jphysa ja

***Under time pressure due to workload***
*wave 2 respondent whether under time pressure due to workload 
gen r2jpress=.
missing_common ep028_, result(r2jpress)
replace r2jpress = .w if r2work ==0
replace r2jpress =1 if ep028_ == 1
replace r2jpress =2 if ep028_ == 2
replace r2jpress =3 if ep028_ == 3
replace r2jpress =4 if ep028_ == 4
label variable r2jpress "r2jpress:w2 r Under pressure due to workload"
label values r2jpress ja

*wave 2 spouse whether under time pressure due to workload
gen s2jpress=.
spouse r2jpress, result(s2jpress) wave(2)
label variable s2jpress "s2jpress:w2 s Under pressure due to workload"
label values s2jpress ja

***ISCO Occupation code for current job***
*wave 2 respondent ISCO code for current job
gen r2jisco = r1jisco if inw2==1 & ep125_ == 1 & ep141dno == 1 & !mi(r1jisco)
missing_common ep016_, result(r2jisco)
replace r2jisco = .w if r2work==0
replace r2jisco = ep016_ if inrange(ep016_,1,9)
replace r2jisco = 0 if ep016_ == 10
label variable r2jisco "r2jisco:w2 r ISCO code for current job"
label values r2jisco isco88

*wave 2 spouse ISCO code for current job
gen s2jisco=.
spouse r2jisco, result(s2jisco) wave(2)
label variable s2jisco "s2jisco:w2 s ISCO code for current job"
label values s2jisco isco88

***NACE Industry code for current job***
*wave 2 respondent NACE code for current job
gen r2jnace = r1jnace if inw2==1 & ep125_ == 1 & ep141dno == 1 & !mi(r1jnace)
missing_common ep018_, result(r2jnace)
replace r2jnace = .w if r2work==0
replace r2jnace = ep018_ if inrange(ep018_,1,14)
label variable r2jnace "r2jnace:w2 r NACE code for current job"
label values r2jnace nace

*wave 2 spouse NACE code for current job
gen s2jnace=.
spouse r2jnace, result(s2jnace) wave(2)
label variable s2jnace "s2jnace:w2 s NACE code for current job"
label values s2jnace nace

***Years of tenure on current job***
*wave 2 respondent years of tenure on current job
gen r2jcten=.
missing_common ep010_, result(r2jcten)
replace r2jcten = .w if r2work == 0
replace r2jcten = .a if r2work == 1 & firstwave<`wv' & ep005_ != 2
replace r2jcten = .i if r2work == 1 & firstwave<`wv' & r1work == 0 & ep005_ == 2 & ep141dno == 1
replace r2jcten = (r2iwy-r1iwy) + r1jcten ///
	if inrange(r2iwy,2000,2010) & inrange(r1iwy,1900,2010) & inrange(r1jcten,0,120) & ep141dno==1
replace r2jcten = r2iwy-ep010_ ///
	if inrange(r2iwy,2000,2010) & inrange(ep010_,1900,2010)
replace r2jcten = .n if r2jcten < 0
label variable r2jcten "r2jcten:w2 r Current Job Tenure"

*wave 2 spouse years of tenure on current job
gen s2jcten=.
spouse r2jcten, result(s2jcten) wave(2)
label variable s2jcten "s2jcten:w2 s Current Job Tenure"

***year last job ended***
*wave 2 respondent year last job ended
gen r2jlasty=.
missing_h r1jlasty r2work if inw2 == 1, result(r2jlasty)
missing_common ep050_, result(r2jlasty)
replace r2jlasty = .n if ep006_ ==5
replace r2jlasty = r1jlasty if inw2==1 & !mi(r1jlasty)
replace r2jlasty = ep050_ if inrange(ep050_,1900,2050)
replace r2jlasty = .w if r2work == 1
replace r2jlasty = .i if r2jlasty < rabyear & inrange(r2jlasty,1900,2050) & inrange(rabyear,1890,2010)
label variable r2jlasty "r2jlasty:w2 r Year Last Worked/not workng"

*wave 2 spouse year last job ended
gen s2jlasty=.
spouse r2jlasty, result(s2jlasty) wave(2)
label variable s2jlasty "s2jlasty:w2 s Year Last Worked/not workng"


***drop cover screen tracker (respondent) file raw variables
drop `employ_all_cv_r'

***drop wave 2 Employment and Pension file raw variables
drop `employ_w2_ep'

******************************************************************************************

	

*set wave number
local wv = 4

***merge with cover screen tracker (respondent) data***
local employ_all_cv_r firstwave
merge 1:1 mergeid using "$tracker", keepusing(`employ_all_cv_r') nogen

***merge with wave 4 Employment and Pension data***
local employ_w4_ep ep002_ ep005_ ep007_ ep009_ ep014_ ep322_  ///
				    ep027_  ep028_ ep050_ ep006_ ep013_ ep321_ ///
				    ep010_ ep141dno ep016_ ep018_ ep125_ ep141dno
merge 1:1 mergeid using "$wave_4_ep", keepusing(`employ_w4_ep') nogen


***Currently working for pay***
*wave 4 respondent working for pay
gen r4work=.
missing_common ep002_ ep005_,result(r4work)
replace r4work = 0 if ep002_ == 5 & ep002_ != 1
replace r4work = 1 if ep005_ == 2 | ep002_ == 1
label variable r4work "r4work:w4 R working for pay"
label values r4work work

*wave 4 spouse working for pay
gen s4work=.
spouse r4work, result(s4work) wave(4)
label variable s4work "s4work:w4 S working for pay"
label values s4work work

*wave 4 respondent works at second job
gen r4work2=.
missing_common ep005_ ep007_, result(r4work2)
replace r4work2=0 if inlist(ep005_,1,3,4,5,97) | ep007_ == 5
replace r4work2=1 if ep007_ == 1
label variable r4work2 "r4work2:w4 R Works at 2nd job"
label values r4work2 work

*wave 4 spouse works at second job
gen s4work2=.
spouse r4work2, result(s4work2) wave(4)
label variable s4work2 "s4work2:w4 S Works at 2nd job"
label values s4work2 work

***Whether self-employed***
*wave 4 respondent whether self-employed
gen r4slfemp=.
missing_common ep002_ ep005_ ep009_, result(r4slfemp)
replace r4slfemp = 0 if ep002_ == 5 | inlist(ep005_,1,3,4,5,97) | ///
												inlist(ep009_,1,2)
replace r4slfemp = 1 if ep009_ ==3
label variable r4slfemp "r4slfemp:w4 r Whether Self-Employed"
label values r4slfemp slfemp

*wave 4 spouse whether self-employed
gen s4slfemp=.
spouse r4slfemp, result(s4slfemp) wave(4)
label variable s4slfemp "s4slfemp:w4 s Whether Self-Employed"
label values s4slfemp slfemp

***labor force status***
*wave 4 respondent labor force status
gen r4lbrf_s=.
missing_common ep005_, result(r4lbrf_s)
replace r4lbrf_s = .o if ep005_== 97
replace r4lbrf_s = 5 if ep005_ == 1
replace r4lbrf_s = 1 if ep005_ == 2
replace r4lbrf_s = 3 if ep005_ == 3
replace r4lbrf_s = 6 if ep005_ == 4
replace r4lbrf_s = 8 if ep005_ == 5
label variable r4lbrf_s "r4lbrf_s:w4 R labor force status"
label values r4lbrf_s lbrf_s

*wave 4 spouse labor force status
gen s4lbrf_s=.
spouse r4lbrf_s, result(s4lbrf_s) wave(4)
label variable s4lbrf_s "s4lbrf_s:w4 S labor force status"
label values s4lbrf_s lbrf_s

***Hours of work per week at current job***
*wave 4 respondent Hours of work per week at main job
gen r4jhours=.
missing_common ep013_, result(r4jhours)
replace r4jhours = .w if r4work==0
replace r4jhours = ep013_ if inrange(ep013_,0,168)
label variable r4jhours "r4jhours:w4 r Hours worked/week main job"

*wave 4 spouse Hours of work per week at main job
gen s4jhours=.
spouse r4jhours, result(s4jhours) wave(4)
label variable s4jhours "s4jhours:w4 s Hours worked/week main job"

*wave 4 respondent hours worked per week at 2nd job
gen r4jhour2=.
missing_common ep321_, result(r4jhour2)
replace r4jhour2 = .w if r4work2==0
replace r4jhour2 = ep321_ if inrange(ep321_,0,168)
label variable r4jhour2 "r4jhour2:w4 r Hours worked/week 2nd job"

*wave 4 spouse hours worked per week at 2nd job
gen s4jhour2=.
spouse r4jhour2, result(s4jhour2) wave(4)
label variable s4jhour2 "s4jhour2:w4 s Hours worked/week 2nd job"

***Weeks worked per year***
*wave 4 respondent weeks worked per year
gen r4jweeks_s=.
missing_common ep014_, result(r4jweeks_s)
replace r4jweeks_s = .w if r4work ==0
replace r4jweeks_s = ep014_*4.3333 if inrange(ep014_,1,12)
label variable r4jweeks_s "r4jweeks_s:w4 r Weeks worked/year main job"

*wave 4 spouse weeks worked per year
gen s4jweeks_s=.
spouse r4jweeks_s, result(s4jweeks_s) wave(4)
label variable s4jweeks_s "s4jweeks_s:w4 s Weeks worked/year main job"

***Weeks worked per year***
*wave 4 respondent weeks worked per year at 2nd job
gen r4jweek2_s=.
missing_common ep322_, result(r4jweek2_s)
replace r4jweek2_s = .w if r4work ==0
replace r4jweek2_s = ep322_*4.3333 if inrange(ep322_,1,12)
label variable r4jweek2_s "r4jweek2_s:w4 r Weeks worked/year 2nd job"

*wave 4 spouse weeks worked per year at 2nd job
gen s4jweek2_s=.
spouse r4jweek2_s, result(s4jweek2_s) wave(4)
label variable s4jweek2_s "s4jweek2_s:w4 s Weeks worked/year 2nd job"

***Current job requires lots of physical effort***
*wave 4 respondent whether job requires physical effort
gen r4jphysa=.
missing_common ep027_, result(r4jphysa)
replace r4jphysa = .w if r4work ==0
replace r4jphysa =1 if ep027_ == 1
replace r4jphysa =2 if ep027_ == 2
replace r4jphysa =3 if ep027_ == 3
replace r4jphysa =4 if ep027_ == 4
label variable r4jphysa "r4jphysa:w4 r Cur job req lots phys effort"
label values r4jphysa ja

*wave 4 spouse whether job requires physical effort
gen s4jphysa=.
spouse r4jphysa, result(s4jphysa) wave(4)
label variable s4jphysa "s4jphysa:w4 s Cur job req lots phys effort"
label values s4jphysa ja

***Under time pressure due to workload***
*wave 4 respondent whether under time pressure due to workload 
gen r4jpress=.
missing_common ep028_, result(r4jpress)
replace r4jpress = .w if r4work ==0
replace r4jpress =1 if ep028_ == 1
replace r4jpress =2 if ep028_ == 2
replace r4jpress =3 if ep028_ == 3
replace r4jpress =4 if ep028_ == 4
label variable r4jpress "r4jpress:w4 r Under pressure due to workload"
label values r4jpress ja

*wave 4 spouse whether under time pressure due to workload
gen s4jpress=.
spouse r4jpress, result(s4jpress) wave(4)
label variable s4jpress "s4jpress:w4 s Under pressure due to workload"
label values s4jpress ja

***ISCO Occupation code for current job***
*wave 4 respondent ISCO code for current job
gen r4jisco=.
replace r4jisco = r1jisco if inw2==0 & inw4 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r1jisco)
replace r4jisco = r2jisco if inw4 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r2jisco)
missing_common ep016_, result(r4jisco)
replace r4jisco = .w if r4work==0
replace r4jisco = ep016_ if inrange(ep016_,1,9)
replace r4jisco = 0 if ep016_ == 10
label variable r4jisco "r4jisco:w4 r ISCO code for current job"
label values r4jisco isco88

*wave 4 spouse ISCO code for current job
gen s4jisco=.
spouse r4jisco, result(s4jisco) wave(4)
label variable s4jisco "s4jisco:w4 s ISCO code for current job"
label values s4jisco isco88

***NACE Industry code for current job***
*wave 4 respondent NACE code for current job
gen r4jnace=.
replace r4jnace = r1jnace if inw2==0 & inw4 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r1jnace)
replace r4jnace = r2jnace if inw4 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r2jnace)
missing_common ep018_, result(r4jnace)
replace r4jnace = .w if r4work==0
replace r4jnace = ep018_ if inrange(ep018_,1,14)
label variable r4jnace "r4jnace:w4 r NACE code for current job"
label values r4jnace nace

*wave 4 spouse NACE code for current job
gen s4jnace=.
spouse r4jnace, result(s4jnace) wave(4)
label variable s4jnace "s4jnace:w4 s NACE code for current job"
label values s4jnace nace

***Years of tenure on current job***
*wave 4 respondent years of tenure on current job
gen r4jcten=.
missing_common ep010_, result(r4jcten)
replace r4jcten = .w if r4work ==0
replace r4jcten = .a if r4work == 1 & firstwave<`wv' & ep005_ != 2
replace r4jcten = .i if r4work == 1 & firstwave<`wv' & r2work == 0 & ep005_ == 2 & ep141dno == 1
replace r4jcten = .i if r4work == 1 & firstwave<`wv' & r1work == 0 & inw2 == 0 & ep005_ == 2 & ep141dno == 1
replace r4jcten = (r4iwy-r1iwy) + r1jcten ///
	if inrange(r4iwy,2000,2020) & inrange(r1iwy,1900,2020) & inrange(r1jcten,0,120) & ep141dno==1
replace r4jcten = (r4iwy-r2iwy) + r2jcten ///
	if inrange(r4iwy,2000,2020) & inrange(r2iwy,1900,2020) & inrange(r2jcten,0,120) & ep141dno==1
replace r4jcten = r4iwy-ep010_ ///
	if inrange(r4iwy,2000,2020) & inrange(ep010_,1900,2020)
replace r4jcten = .n if r4jcten < 0
label variable r4jcten "r4jcten:w4 r Current Job Tenure"

*wave 4 spouse years of tenure on current job
gen s4jcten=.
spouse r4jcten, result(s4jcten) wave(4)
label variable s4jcten "s4jcten:w4 s Current Job Tenure"

***year last job ended***
*wave 4 respondent year last job ended
gen r4jlasty=.
missing_h r1jlasty r2jlasty r4work if inw4 == 1, result(r4jlasty)
missing_common ep050_, result(r4jlasty)
replace r4jlasty = .n if ep006_ ==5
replace r4jlasty = r1jlasty if inw4==1 & !mi(r1jlasty)
replace r4jlasty = r2jlasty if inw4==1 & !mi(r2jlasty)
replace r4jlasty = ep050_ if inrange(ep050_,1900,2050)
replace r4jlasty = .w if r4work == 1
replace r4jlasty = .i if r4jlasty < rabyear & inrange(r4jlasty,1900,2050) & inrange(rabyear,1890,2010)
label variable r4jlasty "r4jlasty:w4 r Year Last Worked/not workng"

*wave 4 spouse year last job ended
gen s4jlasty=.
spouse r4jlasty, result(s4jlasty) wave(4)
label variable s4jlasty "s4jlasty:w4 s Year Last Worked/not workng"


***drop cover screen tracker (respondent) file raw variables
drop `employ_all_cv_r'

***drop wave 4 Employment and Pension file raw variables
drop `employ_w4_ep'

******************************************************************************************

	

*set wave number
local wv = 5

***merge with cover screen tracker (respondent) data***
local employ_all_cv_r firstwave
merge 1:1 mergeid using "$tracker", keepusing(`employ_all_cv_r') nogen

***merge with wave 5 Employment and Pension data***
local employ_w5_ep ep002_ ep005_ ep007_ ep009_ ep014_ ep322_  ///
				    ep027_  ep028_ ep050_ ep006_ ep013_ ep321_ ///
				    ep010_ ep141dno ep016_ ep018_ ep125_ ep141d1 ///
				    ep141d2 ep141d3 ep141d4 ep141d5
merge 1:1 mergeid using "$wave_5_ep", keepusing(`employ_w5_ep') nogen


***Currently working for pay***
*wave 5 respondent working for pay
gen r5work=.
missing_common ep002_ ep005_,result(r5work)
replace r5work = 0 if ep002_ == 5 & ep002_ != 1
replace r5work = 1 if ep005_ == 2 | ep002_ == 1
label variable r5work "r5work:w5 R working for pay"
label values r5work work

*wave 5 spouse working for pay
gen s5work=.
spouse r5work, result(s5work) wave(5)
label variable s5work "s5work:w5 S working for pay"
label values s5work work

*wave 5 respondent works at second job
gen r5work2=.
missing_common ep005_ ep007_, result(r5work2)
replace r5work2=0 if inlist(ep005_,1,3,4,5,97) | ep007_ == 5
replace r5work2=1 if ep007_ == 1
label variable r5work2 "r5work2:w5 R Works at 2nd job"
label values r5work2 work

*wave 5 spouse works at second job
gen s5work2=.
spouse r5work2, result(s5work2) wave(5)
label variable s5work2 "s5work2:w5 S Works at 2nd job"
label values s5work2 work

***Whether self-employed***
*wave 5 respondent whether self-employed
gen r5slfemp=.
missing_common ep002_ ep005_ ep009_, result(r5slfemp)
replace r5slfemp = 0 if ep002_ == 5 | inlist(ep005_,1,3,4,5,97) | ///
												inlist(ep009_,1,2)
replace r5slfemp = 1 if ep009_ ==3
label variable r5slfemp "r5slfemp:w5 r Whether Self-Employed"
label values r5slfemp slfemp

*wave 5 spouse whether self-employed
gen s5slfemp=.
spouse r5slfemp, result(s5slfemp) wave(5)
label variable s5slfemp "s5slfemp:w5 s Whether Self-Employed"
label values s5slfemp slfemp

***labor force status***
*wave 5 respondent labor force status
gen r5lbrf_s=.
missing_common ep005_, result(r5lbrf_s)
replace r5lbrf_s = .o if ep005_== 97
replace r5lbrf_s = 5 if ep005_ == 1
replace r5lbrf_s = 1 if ep005_ == 2
replace r5lbrf_s = 3 if ep005_ == 3
replace r5lbrf_s = 6 if ep005_ == 4
replace r5lbrf_s = 8 if ep005_ == 5
label variable r5lbrf_s "r5lbrf_s:w5 R labor force status"
label values r5lbrf_s lbrf_s

*wave 5 spouse labor force status
gen s5lbrf_s=.
spouse r5lbrf_s, result(s5lbrf_s) wave(5)
label variable s5lbrf_s "s5lbrf_s:w5 S labor force status"
label values s5lbrf_s lbrf_s

***Hours of work per week at current job***
*wave 5 respondent Hours of work per week at main job
gen r5jhours=.
missing_common ep013_, result(r5jhours)
replace r5jhours = .w if r5work==0
replace r5jhours = ep013_ if inrange(ep013_,0,168)
label variable r5jhours "r5jhours:w5 r Hours worked/week main job"

*wave 5 spouse Hours of work per week at main job
gen s5jhours=.
spouse r5jhours, result(s5jhours) wave(5)
label variable s5jhours "s5jhours:w5 s Hours worked/week main job"

*wave 5 respondent hours worked per week at 2nd job
gen r5jhour2=.
missing_common ep321_, result(r5jhour2)
replace r5jhour2 = .w if r5work2==0
replace r5jhour2 = ep321_ if inrange(ep321_,0,168)
label variable r5jhour2 "r5jhour2:w5 r Hours worked/week 2nd job"

*wave 5 spouse hours worked per week at 2nd job
gen s5jhour2=.
spouse r5jhour2, result(s5jhour2) wave(5)
label variable s5jhour2 "s5jhour2:w5 s Hours worked/week 2nd job"

***Weeks worked per year***
*wave 5 respondent weeks worked per year
gen r5jweeks_s=.
missing_common ep014_, result(r5jweeks_s)
replace r5jweeks_s = .w if r5work ==0
replace r5jweeks_s = ep014_*4.3333 if inrange(ep014_,1,12)
label variable r5jweeks_s "r5jweeks_s:w5 r Weeks worked/year main job"

*wave 5 spouse weeks worked per year
gen s5jweeks_s=.
spouse r5jweeks_s, result(s5jweeks_s) wave(5)
label variable s5jweeks_s "s5jweeks_s:w5 s Weeks worked/year main job"

***Weeks worked per year***
*wave 5 respondent weeks worked per year at 2nd job
gen r5jweek2_s=.
missing_common ep322_, result(r5jweek2_s)
replace r5jweek2_s = .w if r5work ==0
replace r5jweek2_s = ep322_*4.3333 if inrange(ep322_,1,12)
label variable r5jweek2_s "r5jweek2_s:w5 r Weeks worked/year 2nd job"

*wave 5 spouse weeks worked per year at 2nd job
gen s5jweek2_s=.
spouse r5jweek2_s, result(s5jweek2_s) wave(5)
label variable s5jweek2_s "s5jweek2_s:w5 s Weeks worked/year 2nd job"

***Current job requires lots of physical effort***
*wave 5 respondent whether job requires physical effort
gen r5jphysa=.
missing_h r1jphysa r2jphysa r4jphysa if ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0, result(r5jphysa)
missing_common ep027_, result(r5jphysa)
replace r5jphysa = .s if firstwave==`wv'
replace r5jphysa = .w if r5work==0
replace r5jphysa = r1jphysa if !missing(r1jphysa) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r5jphysa = r2jphysa if !missing(r2jphysa) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r5jphysa = r4jphysa if !missing(r4jphysa) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r5jphysa =1 if ep027_ == 1
replace r5jphysa =2 if ep027_ == 2
replace r5jphysa =3 if ep027_ == 3
replace r5jphysa =4 if ep027_ == 4
label variable r5jphysa "r5jphysa:w5 r Cur job req lots phys effort"
label values r5jphysa ja

*wave 5 spouse whether job requires physical effort
gen s5jphysa=.
spouse r5jphysa, result(s5jphysa) wave(5)
label variable s5jphysa "s5jphysa:w5 s Cur job req lots phys effort"
label values s5jphysa ja

***Under time pressure due to workload***
*wave 5 respondent whether under time pressure due to workload 
gen r5jpress=.
missing_h r1jpress r2jpress r4jpress if ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0, result(r5jpress)
missing_common ep028_, result(r5jpress)
replace r5jpress = .s if firstwave==`wv'
replace r5jpress = .w if r5work==0
replace r5jpress = r1jpress if !missing(r1jpress) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r5jpress = r2jpress if !missing(r2jpress) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r5jpress = r4jpress if !missing(r4jpress) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r5jpress =1 if ep028_ == 1
replace r5jpress =2 if ep028_ == 2
replace r5jpress =3 if ep028_ == 3
replace r5jpress =4 if ep028_ == 4
label variable r5jpress "r5jpress:w5 r Under pressure due to workload"
label values r5jpress ja

*wave 5 spouse whether under time pressure due to workload
gen s5jpress=.
spouse r5jpress, result(s5jpress) wave(5)
label variable s5jpress "s5jpress:w5 s Under pressure due to workload"
label values s5jpress ja

***ISCO Occupation code for current job***
*wave 5 respondent ISCO code for current job
gen r5jisco=.
replace r5jisco = r1jisco if inw2 == 0 & inw4 == 0 & inw5 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r1jisco)
replace r5jisco = r2jisco if inw4 == 0 & inw5 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r2jisco)
replace r5jisco = r4jisco if inw5 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r4jisco)
missing_common ep016_, result(r5jisco)
replace r5jisco = .w if r5work==0 
replace r5jisco = ep016_ if inrange(ep016_,1,9)
replace r5jisco = 0 if ep016_ == 10
label variable r5jisco "r5jisco:w5 r ISCO code for current job"
label values r5jisco isco88

*wave 5 spouse ISCO code for current job
gen s5jisco=.
spouse r5jisco, result(s5jisco) wave(5)
label variable s5jisco "s5jisco:w5 s ISCO code for current job"
label values s5jisco isco88

***NACE Industry code for current job***
*wave 5 respondent NACE code for current job
gen r5jnace=.
replace r5jnace = r1jnace if inw2 == 0 & inw4 == 0 & inw5 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r1jnace)
replace r5jnace = r2jnace if inw4 == 0 & inw5 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r2jnace)
replace r5jnace = r4jnace if inw5 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r2jnace)
missing_common ep018_, result(r5jnace)
replace r5jnace = .w if r5work==0 
replace r5jnace = ep018_ if inrange(ep018_,1,14)
label variable r5jnace "r5jnace:w5 r NACE code for current job"
label values r5jnace nace

*wave 5 spouse NACE code for current job
gen s5jnace=.
spouse r5jnace, result(s5jnace) wave(5)
label variable s5jnace "s5jnace:w5 s NACE code for current job"
label values s5jnace nace

***Years of tenure on current job***
*wave 5 respondent years of tenure on current job
gen r5jcten=.
missing_common ep010_, result(r5jcten)
replace r5jcten = .w if r5work ==0
replace r5jcten = .a if r5work == 1 & firstwave<`wv' & ep005_ != 2
replace r5jcten = .i if r5work == 1 & firstwave<`wv' & r4work == 0 & ep005_ == 2 & ep141dno == 1
replace r5jcten = .i if r5work == 1 & firstwave<`wv' & r2work == 0 & inw4 == 0 & ep005_ == 2 & ep141dno == 1
replace r5jcten = .i if r5work == 1 & firstwave<`wv' & r1work == 0 & inw2 == 0 & inw4 == 0 & ep005_ == 2 & ep141dno == 1
replace r5jcten = (r5iwy-r1iwy) + r1jcten ///
	if inrange(r5iwy,2000,2020) & inrange(r1iwy,1900,2020) & inrange(r1jcten,0,120) & ep141dno==1
replace r5jcten = (r5iwy-r2iwy) + r2jcten ///
	if inrange(r5iwy,2000,2020) & inrange(r2iwy,1900,2020) & inrange(r2jcten,0,120) & ep141dno==1
replace r5jcten = (r5iwy-r4iwy) + r4jcten ///
	if inrange(r5iwy,2000,2020) & inrange(r4iwy,1900,2020) & inrange(r4jcten,0,120) & ep141dno==1
replace r5jcten = r5iwy-ep010_ ///
	if inrange(r5iwy,2000,2020) & inrange(ep010_,1900,2020)
replace r5jcten = .n if r5jcten < 0
label variable r5jcten "r5jcten:w5 r Current Job Tenure"

*wave 5 spouse years of tenure on current job
gen s5jcten=.
spouse r5jcten, result(s5jcten) wave(5)
label variable s5jcten "s5jcten:w5 s Current Job Tenure"

***year last job ended***
*wave 5 respondent year last job ended
gen r5jlasty=.
missing_h r1jlasty r2jlasty r4jlasty if inw5 == 1, result(r5jlasty)
missing_common ep050_, result(r5jlasty)
replace r5jlasty = .n if ep006_ ==5
replace r5jlasty = r1jlasty if inw5==1 & !mi(r1jlasty)
replace r5jlasty = r2jlasty if inw5==1 & !mi(r2jlasty)
replace r5jlasty = r4jlasty if inw5==1 & !mi(r4jlasty)
replace r5jlasty = ep050_ if inrange(ep050_,1900,2050)
replace r5jlasty = .w if r5work == 1
replace r5jlasty = .i if r5jlasty < rabyear & inrange(r5jlasty,1900,2050) & inrange(rabyear,1890,2010)
label variable r5jlasty "r5jlasty:w5 r Year Last Worked/not workng"

*wave 5 spouse year last job ended
gen s5jlasty=.
spouse r5jlasty, result(s5jlasty) wave(5)
label variable s5jlasty "s5jlasty:w5 s Year Last Worked/not workng"


***drop cover screen tracker (respondent) file raw variables
drop `employ_all_cv_r'

***drop wave 2 Employment and Pension file raw variables
drop `employ_w5_ep'

******************************************************************************************

	

*set wave number
local wv = 6 

***merge with cover screen tracker (respondent) data***
local employ_all_cv_r firstwave
merge 1:1 mergeid using "$tracker", keepusing(`employ_all_cv_r') nogen

***merge with wave 6 Employment and Pension data***
local employ_w6_ep ep002_ ep005_ ep007_ ep009_ ep322_  ///
				    ep027_  ep028_ ep050_ ep006_ ep013_ ep321_ ///
				    ep010_ ep141dno ep616 ep018_ ep125_ ep141d1 ///
				    ep141d2 ep141d3 ep141d4 ep141d5
merge 1:1 mergeid using "$wave_6_ep", keepusing(`employ_w6_ep') nogen


***Currently working for pay***
*wave 6 respondent working for pay
gen r6work=.
missing_common ep002_ ep005_,result(r6work)
replace r6work = 0 if ep002_ == 5 & ep002_ != 1 
replace r6work = 1 if ep005_ == 2 | ep002_ == 1
label variable r6work "r6work:w6 R working for pay"
label values r6work work
 
*wave 6 spouse working for pay
gen s6work=.
spouse r6work, result(s6work) wave(6)
label variable s6work "s6work:w6 S working for pay"
label values s6work work

*wave 6 respondent works at second job
gen r6work2=.
missing_common ep005_ ep007_, result(r6work2)
replace r6work2=0 if inlist(ep005_,1,3,4,5,97) | ep007_ == 5
replace r6work2=1 if ep007_ == 1
label variable r6work2 "r6work2:w6 R Works at 2nd job"
label values r6work2 work
 
*wave 6 spouse works at second job
gen s6work2=.
spouse r6work2, result(s6work2) wave(6)
label variable s6work2 "s6work2:w6 S Works at 2nd job"
label values s6work2 work

***Whether self-employed***
*wave 6 respondent whether self-employed
gen r6slfemp=.
missing_common ep002_ ep005_ ep009_, result(r6slfemp)
replace r6slfemp = 0 if ep002_ == 5 | inlist(ep005_,1,3,4,5,97) | ///
												inlist(ep009_,1,2)
replace r6slfemp = 1 if ep009_ ==3
label variable r6slfemp "r6slfemp:w6 r Whether Self-Employed"
label values r6slfemp slfemp

*wave 6 spouse whether self-employed
gen s6slfemp=.
spouse r6slfemp, result(s6slfemp) wave(6)
label variable s6slfemp "s6slfemp:w6 s Whether Self-Employed"
label values s6slfemp slfemp

***labor force status***
*wave 6 respondent labor force status
gen r6lbrf_s=.
missing_common ep005_, result(r6lbrf_s)
replace r6lbrf_s = .o if ep005_== 97
replace r6lbrf_s = 5 if ep005_ == 1
replace r6lbrf_s = 1 if ep005_ == 2
replace r6lbrf_s = 3 if ep005_ == 3
replace r6lbrf_s = 6 if ep005_ == 4
replace r6lbrf_s = 8 if ep005_ == 5
label variable r6lbrf_s "r6lbrf_s:w6 R labor force status"
label values r6lbrf_s lbrf_s
 
*wave 6 spouse labor force status
gen s6lbrf_s=.
spouse r6lbrf_s, result(s6lbrf_s) wave(6)
label variable s6lbrf_s "s6lbrf_s:w6 S labor force status"
label values s6lbrf_s lbrf_s

***Hours of work per week at current job***
*wave 6 respondent Hours of work per week at main job
gen r6jhours=.
missing_common ep013_, result(r6jhours)
replace r6jhours = .w if r6work==0
replace r6jhours = ep013_ if inrange(ep013_,0,168)
label variable r6jhours "r6jhours:w6 r Hours worked/week main job"
 
*wave 6 spouse Hours of work per week at main job
gen s6jhours=.
spouse r6jhours, result(s6jhours) wave(6)
label variable s6jhours "s6jhours:w6 s Hours worked/week main job"

*wave 6 respondent hours worked per week at 2nd job
gen r6jhour2=.
missing_common ep321_, result(r6jhour2)
replace r6jhour2 = .w if r6work2==0
replace r6jhour2 = ep321_ if inrange(ep321_,0,168)
label variable r6jhour2 "r6jhour2:w6 r Hours worked/week 2nd job"

*wave 6 spouse hours worked per week at 2nd job
gen s6jhour2=.
spouse r6jhour2, result(s6jhour2) wave(6)
label variable s6jhour2 "s6jhour2:w6 s Hours worked/week 2nd job"

***Weeks worked per year*** 
*wave 6 respondent weeks worked per year at 2nd job
gen r6jweek2_s=.
missing_common ep322_, result(r6jweek2_s)
replace r6jweek2_s = .w if r6work ==0
replace r6jweek2_s = ep322_*4.3333 if inrange(ep322_,1,12)
label variable r6jweek2_s "r6jweek2_s:w6 r Weeks worked/year 2nd job"

*wave 6 spouse weeks worked per year at 2nd job
gen s6jweek2_s=.
spouse r6jweek2_s, result(s6jweek2_s) wave(6)
label variable s6jweek2_s "s6jweek2_s:w6 s Weeks worked/year 2nd job"

***Current job requires lots of physical effort***
*wave 6 respondent whether job requires physical effort
gen r6jphysa=.
missing_h r1jphysa r2jphysa r4jphysa r5jphysa if ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0, result(r6jphysa)
missing_common ep027_, result(r6jphysa)
replace r6jphysa = .s if firstwave==`wv'
replace r6jphysa = .w if r6work==0
replace r6jphysa = r1jphysa if !missing(r1jphysa) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r6jphysa = r2jphysa if !missing(r2jphysa) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r6jphysa = r4jphysa if !missing(r4jphysa) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r6jphysa = r5jphysa if !missing(r5jphysa) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r6jphysa =1 if ep027_ == 1
replace r6jphysa =2 if ep027_ == 2
replace r6jphysa =3 if ep027_ == 3
replace r6jphysa =4 if ep027_ == 4
label variable r6jphysa "r6jphysa:w6 r Cur job req lots phys effort"
label values r6jphysa ja

*wave 6 spouse whether job requires physical effort
gen s6jphysa=.
spouse r6jphysa, result(s6jphysa) wave(6)
label variable s6jphysa "s6jphysa:w6 s Cur job req lots phys effort"
label values s6jphysa ja

***Under time pressure due to workload***
*wave 6 respondent whether under time pressure due to workload 
gen r6jpress=.
missing_h r1jpress r2jpress r4jpress r5jpress if ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0, result(r6jpress)
missing_common ep028_, result(r6jpress)
replace r6jpress = .s if firstwave==`wv'
replace r6jpress = .w if r6work==0
replace r6jpress = r1jpress if !missing(r1jpress) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r6jpress = r2jpress if !missing(r2jpress) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r6jpress = r4jpress if !missing(r4jpress) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r6jpress = r5jpress if !missing(r5jpress) & ep141d1 == 0 & ep141d2 == 0 & ep141d3 == 0 & ep141d4 == 0 & ep141d5 == 0 
replace r6jpress =1 if ep028_ == 1
replace r6jpress =2 if ep028_ == 2
replace r6jpress =3 if ep028_ == 3
replace r6jpress =4 if ep028_ == 4
label variable r6jpress "r6jpress:w6 r Under pressure due to workload"
label values r6jpress ja

*wave 6 spouse whether under time pressure due to workload
gen s6jpress=.
spouse r6jpress, result(s6jpress) wave(6)
label variable s6jpress "s6jpress:w6 s Under pressure due to workload"
label values s6jpress ja

***Years of tenure on current job***
*wave 6 respondent years of tenure on current job
gen r6jcten=.
missing_common ep010_, result(r6jcten)
replace r6jcten = .w if r6work ==0
replace r6jcten = .a if r6work == 1 & firstwave<`wv' & ep005_ != 2
replace r6jcten = .i if r6work == 1 & firstwave<`wv' & r5work == 0 & ep005_ == 2 & ep141dno == 1
replace r6jcten = .i if r6work == 1 & firstwave<`wv' & r4work == 0 & inw5 == 0 & ep005_ == 2 & ep141dno == 1
replace r6jcten = .i if r6work == 1 & firstwave<`wv' & r2work == 0 & inw5 == 0 & inw4 == 0 & ep005_ == 2 & ep141dno == 1
replace r6jcten = .i if r6work == 1 & firstwave<`wv' & r1work == 0 & inw5 == 0 & inw4 == 0 & inw2 == 0 & ep005_ == 2 & ep141dno == 1
replace r6jcten = (r6iwy-r1iwy) + r1jcten ///
	if inrange(r6iwy,2000,2020) & inrange(r1iwy,1900,2020) & inrange(r1jcten,0,120) & ep141dno==1
replace r6jcten = (r6iwy-r2iwy) + r2jcten ///
	if inrange(r6iwy,2000,2020) & inrange(r2iwy,1900,2020) & inrange(r2jcten,0,120) & ep141dno==1
replace r6jcten = (r6iwy-r4iwy) + r4jcten ///
	if inrange(r6iwy,2000,2020) & inrange(r4iwy,1900,2020) & inrange(r4jcten,0,120) & ep141dno==1
replace r6jcten = (r6iwy-r5iwy) + r5jcten ///
	if inrange(r6iwy,2000,2020) & inrange(r5iwy,1900,2020) & inrange(r5jcten,0,120) & ep141dno==1
replace r6jcten = r6iwy-ep010_ ///
	if inrange(r6iwy,2000,2020) & inrange(ep010_,1900,2020)
replace r6jcten = .n if r6jcten < 0
label variable r6jcten "r6jcten:w6 r Current Job Tenure"

*wave 6 spouse years of tenure on current job
gen s6jcten=.
spouse r6jcten, result(s6jcten) wave(6)
label variable s6jcten "s6jcten:w6 s Current Job Tenure"

***ISCO Occupation code for current job***
*wave 6 respondent ISCO code for current job (NOT IN WAVE 6) 
gen r6jisco=.
replace r6jisco = r1jisco if inw2 == 0 & inw4 == 0 & inw5 == 0 & inw6 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r1jisco)
replace r6jisco = r2jisco if inw4 == 0 & inw5 == 0 & inw6 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r2jisco)
replace r6jisco = r4jisco if inw5 == 0 & inw6 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r4jisco)
replace r6jisco = r5jisco if inw6 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r5jisco)
missing_common ep616, result(r6jisco)
replace r6jisco = .w if r6work==0 
replace r6jisco = .d if ep616 == -1
gen isco_mg = int(ep616/1000) if ep616!=-1
replace r6jisco = isco_mg if inrange(isco_mg,0,9)
drop isco_mg
label variable r6jisco "r6jisco:w6 r ISCO code for current job"
label values r6jisco isco88

*wave 5 spouse ISCO code for current job
gen s6jisco=.
spouse r6jisco, result(s6jisco) wave(6)
label variable s6jisco "s6jisco:w6 s ISCO code for current job"
label values s6jisco isco88

***NACE Industry code for current job***
*wave 5 respondent NACE code for current job
gen r6jnace=.
replace r6jnace = r1jnace if inw2 == 0 & inw4 == 0 & inw5 == 0 & inw6 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r1jnace)
replace r6jnace = r2jnace if inw4 == 0 & inw5 == 0 & inw6 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r2jnace)
replace r6jnace = r4jnace if inw5 == 0 & inw6 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r4jnace)
replace r6jnace = r5jnace if inw6 == 1 & ep125_ == 1 & ep141dno == 1 & !mi(r5jnace)
missing_common ep018_, result(r6jnace)
replace r6jnace = .w if r6work==0 
replace r6jnace = ep018_ if inrange(ep018_,1,14)
label variable r6jnace "r6jnace:w6 r NACE code for current job"
label values r6jnace nace

*wave 5 spouse NACE code for current job
gen s6jnace=.
spouse r6jnace, result(s6jnace) wave(6)
label variable s6jnace "s6jnace:w6 s NACE code for current job"
label values s6jnace nace

***year last job ended***
*wave 5 respondent year last job ended
gen r6jlasty=.
missing_h r1jlasty r2jlasty r4jlasty r5jlasty if inw6 == 1, result(r6jlasty)
missing_common ep050_, result(r6jlasty)
replace r6jlasty = .n if ep006_ ==5
replace r6jlasty = r1jlasty if inw6==1 & !mi(r1jlasty)
replace r6jlasty = r2jlasty if inw6==1 & !mi(r2jlasty)
replace r6jlasty = r4jlasty if inw6==1 & !mi(r4jlasty)
replace r6jlasty = r5jlasty if inw6==1 & !mi(r5jlasty)
replace r6jlasty = ep050_ if inrange(ep050_,1900,2050)
replace r6jlasty = .w if r6work == 1
replace r6jlasty = .i if r6jlasty < rabyear & inrange(r6jlasty,1900,2050) & inrange(rabyear,1890,2010)
label variable r6jlasty "r6jlasty:w6 r Year Last Worked/not workng"

*wave 5 spouse year last job ended
gen s6jlasty=.
spouse r6jlasty, result(s6jlasty) wave(6)
label variable s6jlasty "s6jlasty:w6 s Year Last Worked/not workng"


***drop cover screen tracker (respondent) file raw variables
drop `employ_all_cv_r'

***drop wave 2 Employment and Pension file raw variables
drop `employ_w6_ep'

******************************************************************************************

	


*Retired
label define ret ///
    0 "0.Not retired empstat" ///
   1 "1.retired empstat" ///
   .m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr" 

label define retear ///
   0 "0.No" ///
   1 "1.Yes" ///
   .w ".w:not working" ///
   .m ".m:Missing" ///
   .d ".d:DK" ///
   .r ".r:Refuse" ///
   .u ".u:Unmar" ///
   .v ".v:Sp Nr" 




 
******************************************************************************************

 
	

***merge with wave 1 retire data***
local retire_w1_ep  ep005_ ep006_ ep049_ ep050_ ep036_
merge 1:1 mergeid using "$wave_1_ep", keepusing(`retire_w1_ep') 
drop _merge

***merge with wave 1 expectations data***
local retire_w1_ex  ex002_ ex004_ ex005_ ex006_ ex009_ ex009age ex010_ ex011_ 
merge 1:1 mergeid using "$wave_1_ex", keepusing(`retire_w1_ex')
drop _merge

***merge with wave 1 cv_r data***
local retire_w1_cv_r yrbirth
merge 1:m mergeid using "$wave_1_cv_r", keepusing(`retire_w1_cv_r') nogen
drop if mergeid == "no int w.1"


***Whether retired***
*wave 1 respondent whether retired
gen r1retemp=.
missing_common ep005_,result(r1retemp)
replace r1retemp=1 if ep005_ == 1
replace r1retemp=0 if inlist(ep005_,2,3,4,5,97)
label variable r1retemp "r1retemp:w1 R gives Retired in empstat"
label values r1retemp ret

*wave 1 spouse whether retired
gen s1retemp=.
spouse r1retemp, result(s1retemp) wave(1)
label variable s1retemp "s1retemp:w1 S gives Retired in empstat"
label values s1retemp ret

*wave 1 respondent retire year
gen r1retyr=.
missing_common ep050_,result(r1retyr)
replace r1retyr=.n if inlist(ep005_,2,3,4,5,97)
replace r1retyr=ep050_ if inrange(ep050_,1900, 2100) 
replace r1retyr=.i if ep050_ < yrbirth & inrange(ep050_,1900,2100) & inrange(yrbirth,1890,2010)
label variable r1retyr "r1retyr:w1 r Year retired if retired"

*wave 1 spouse retire year
gen s1retyr=.
spouse r1retyr, result(s1retyr) wave(1)
label variable s1retyr "s1retyr:w1 s Year retired if retired"

***early retire***
*wae 1 respondent early retire
gen r1retear=.
missing_common ep036_,result(r1retear)
replace r1retear=1 if ep036_ == 1
replace r1retear=0 if ep036_==5
replace r1retear=.w if ep005_!=. & (ep005_ >2|ep005_==1)
label variable r1retear "r1retear:w1 R look for early retirement"
label values r1retear retear

*wave 1 spouse early retire
gen s1retear=.
spouse r1retear, result(s1retear) wave(1)
label variable s1retear "s1retear:w1 S look for early retirement"
label values s1retear retear

***Probability Receive Inheritance in Next 10 Years***
*wave 1 respondent probability receive inheritance in next 10 yrs
gen r1inher = .
missing_common ex002_, result(r1inher)
replace r1inher = .p if inlist(r1proxy,1,2,.d,.m,.r) & r1inher == .m
replace r1inher = ex002_ if inrange(ex002_,0,100)
label variable r1inher "r1inher:w1 R prob inheritance next 10 yr"

*wave 1 spouse probability receive inheritance in next 10 yrs
gen s1inher = .
spouse r1inher, result(s1inher) wave(1)
label variable s1inher "s1inher:w1 S prob inheritance next 10 yr"

***Probability Leave Bequest 50K+ Euro***
*wave 1 respondent probability leave bequest 50K+ Euro
gen r1beq50p = .
missing_common ex004_, result(r1beq50p)
replace r1beq50p = .p if inlist(r1proxy,1,2,.d,.m,.r) & r1beq50p == .m
replace r1beq50p = ex004_ if inrange(ex004_,0,100)
label variable r1beq50p "r1beq50p:w1 R prob leave bequest 50k+ euro"

*wave 1 spouse probability leave beqest 50K+ Euro
gen s1beq50p = .
spouse r1beq50p, result(s1beq50p) wave(1)
label variable s1beq50p "s1beq50p:w1 S prob leave bequest 50k+ euro"

***Probability Leave Bequest 150K+ Euro***
*wave 1 respondent probability leave bequest 150K+ Euro
gen r1beq150p = .
missing_common ex006_, result(r1beq150p)
replace r1beq150p = .p if inlist(r1proxy,1,2,.d,.m,.r) & r1beq150p == .m
replace r1beq150p = ex006_ if inrange(ex006_,0,100)
replace r1beq150p = 0 if ex004_ == 0
label variable r1beq150p "r1beq150p:w1 R prob leave bequest 150k+ euro"

*wave 1 spouse probability leave beqest 150K+ Euro
gen s1beq150p = .
spouse r1beq150p, result(s1beq150p) wave(1)
label variable s1beq150p "s1beq150p:w1 S prob leave bequest 150k+ euro"

***Probability Leave Bequest Any***
*wave 1 respondent probability leave bequest any
gen r1beqany = .
missing_common ex005_, result(r1beqany)
replace r1beqany = .s if inrange(ex004_,1,100)
replace r1beqany = .p if inlist(r1proxy,1,2,.d,.m,.r) & r1beqany == .m
replace r1beqany = ex005_ if inrange(ex005_,0,100)
label variable r1beqany "r1beqany:w1 R prob leave bequest any"

*wave 1 spouse probability leave bequest any
gen s1beqany = .
spouse r1beqany, result(s1beqany) wave(1)
label variable s1beqany "s1beqany:w1 S prob leave bequest any"

***Probability Live to Specific Age***
*wave 1 respondent probability live to specific age
gen r1liv10 = .
missing_common ex009_, result(r1liv10)
replace r1liv10 = .p if inlist(r1proxy,1,2,.d,.m,.r) & r1liv10 == .m
replace r1liv10 = ex009_ if inrange(ex009_,0,100)
label variable r1liv10 "r1liv10:w1 R probability to live 75-120"

*wave 1 spouse probability live to specific age
gen s1liv10 = .
spouse r1liv10, result(s1liv10) wave(1)
label variable s1liv10 "s1liv10:w1 S probability to live 75-120"

***Age Used in Probability Live to Specific Age***
*wave 1 respondent age used in probability live to specific age
gen r1liv10a = .
missing_common ex009age, result(r1liv10a)
replace r1liv10a = .p if inlist(r1proxy,1,2,.d,.m,.r) & r1liv10a == .m
replace r1liv10a = ex009age if inrange(ex009age,75,120)
label variable r1liv10a "r1liv10a:w1 R age used in live 75-120"

*wave 1 spouse age used in probability live to specific age
gen s1liv10a = .
spouse r1liv10a, result(s1liv10a) wave(1)
label variable s1liv10a "s1liv10a:w1 S age used in live 75-120"

***Probability Better Standard of Living in 5 Years***
*wave 1 respondent probability better standard of living in 5 years
gen r1retlivb5 = .
missing_common ex010_, result(r1retlivb5)
replace r1retlivb5 = .p if inlist(r1proxy,1,2,.d,.m,.r) & r1retlivb5 == .m
replace r1retlivb5 = ex010_ if inrange(ex010_,0,100)
label variable r1retlivb5 "r1retlivb5:w1 R prob better std living 5 yrs"

*wave 1 spouse probability better standard of living in 5 years
gen s1retlivb5 = .
spouse r1retlivb5, result(s1retlivb5) wave(1)
label variable s1retlivb5 "s1retlivb5:w1 S prob better std living 5 yrs"

***Probability Worse Standard of Living in 5 Years***
*wave 1 respondent probability worse standard of living in 5 years
gen r1retlivw5 = .
missing_common ex011_, result(r1retlivw5)
replace r1retlivw5 = .p if inlist(r1proxy,1,2,.d,.m,.r) & r1retlivw5 == .m
replace r1retlivw5 = ex011_ if inrange(ex011_,0,100)
label variable r1retlivw5 "r1retlivw5:w1 R prob worse std living 5 yrs"

*wave 1 spouse probability worse standard of living in 5 years
gen s1retlivw5 = .
spouse r1retlivw5, result(s1retlivw5) wave(1)
label variable s1retlivw5 "s1retlivw5:w1 S prob worse std living 5 yrs"


***drop wave 1 retire file raw variables
drop `retire_w1_ep'

***drop wave 1 expectations file raw variables
drop `retire_w1_ex'

***drop wave 1 demog file raw variables
drop `retire_w1_cv_r'


******************************************************************************************

	

***merge with wave 2 retire data***
local retire_w2_ep  ep005_ ep049_ ep050_ ep328_ ep329_ ep036_
merge 1:1 mergeid using "$wave_2_ep", keepusing(`retire_w2_ep') nogen

***merge with wave 2 expectations data***
local retire_w2_ex  ex002_ ex004_ ex005_ ex006_ ex009_ ex009age ex010_ ex011_ ex025_ 
merge 1:1 mergeid using "$wave_2_ex", keepusing(`retire_w2_ex') nogen

***merge with wave 2 cv_r data***
local retire_w2_cv_r yrbirth
merge 1:m mergeid using "$wave_2_cv_r", keepusing(`retire_w2_cv_r') nogen
drop if mergeid == "no int w.2"


***Whether retired***
*wave 2 respondent whether retired
gen r2retemp=.
missing_common ep005_,result(r2retemp)
replace r2retemp=1 if ep005_ == 1
replace r2retemp=0 if inlist(ep005_,2,3,4,5,97)
label variable r2retemp "r2retemp:w2 R gives Retired in empstat"
label values r2retemp ret

*wave 2 spouse whether retired
gen s2retemp=.
spouse r2retemp, result(s2retemp) wave(2)
label variable s2retemp "s2retemp:w2 S gives Retired in empstat"
label values s2retemp ret

*wave 2 respondent retire year
gen r2retyr=.
missing_common ep050_ ep329_,result(r2retyr)
replace r2retyr=.n if inlist(ep005_,2,3,4,5,97)
replace r2retyr=ep050_ if ep005_==1 & inrange(ep050_,1900,2100)
replace r2retyr=ep329_ if ep005_==1 & inrange(ep329_,1900, 2100) 
replace r2retyr=.i if ep329_ < yrbirth & inrange(ep329_,1900,2100) & inrange(yrbirth,1890,2010)
label variable r2retyr "r2retyr:w2 r Year retired if retired"


*wave 2 spouse retire year
gen s2retyr=.
spouse r2retyr, result(s2retyr) wave(2)
label variable s2retyr "s2retyr:w2 s Year retired if retired"

*wave 2 respondent retire month
gen r2retmon=.
missing_common ep328_,result(r2retmon)
replace r2retmon=.n if inlist(ep005_,2,3,4,5,97)
replace r2retmon=ep328_ if ep005_==1 & inrange(ep328_,0,12) 
label variable r2retmon "r2retmon:w2 r Month retired if retired"

*wave 2 spouse retire month
gen s2retmon=.
spouse r2retmon, result(s2retmon) wave(2)
label variable s2retmon "s2retmon:w2 s Month retired if retired"

***early retire***
*wae 2 respondent early retire
gen r2retear=.
missing_common ep036_,result(r2retear)
replace r2retear=1 if ep036_ == 1
replace r2retear=0 if ep036_==5
replace r2retear=.w if ep005_!=. & (ep005_ >2|ep005_==1)
label variable r2retear "r2retear:w2 R look for early retirement"
label values r2retear retear

*wave 2 spouse early retire
gen s2retear=.
spouse r2retear, result(s2retear) wave(2)
label variable s2retear "s2retear:w2 S look for early retirement"
label values s2retear retear

***Probability Receive Inheritance in Next 10 Years***
*wave 2 respondent probability receive inheritance in next 10 yrs
gen r2inher = .
missing_common ex002_, result(r2inher)
replace r2inher = .p if inlist(r2proxy,1,2,.d,.m,.r) & r2inher == .m
replace r2inher = ex002_ if inrange(ex002_,0,100)
label variable r2inher "r2inher:w2 R prob inheritance next 10 yr"

*wave 2 spouse probability receive inheritance in next 10 yrs
gen s2inher = .
spouse r2inher, result(s2inher) wave(2)
label variable s2inher "s2inher:w2 S prob inheritance next 10 yr"

***Probability Leave Bequest 50K+ Euro***
*wave 2 respondent probability leave bequest 50K+ Euro
gen r2beq50p = .
missing_common ex004_, result(r2beq50p)
replace r2beq50p = .p if inlist(r2proxy,1,2,.d,.m,.r) & r2beq50p == .m
replace r2beq50p = ex004_ if inrange(ex004_,0,100)
label variable r2beq50p "r2beq50p:w2 R prob leave bequest 50k+ euro"

*wave 2 spouse probability leave beqest 50K+ Euro
gen s2beq50p = .
spouse r2beq50p, result(s2beq50p) wave(2)
label variable s2beq50p "s2beq50p:w2 S prob leave bequest 50k+ euro"

***Probability Leave Bequest 150K+ Euro***
*wave 2 respondent probability leave bequest 150K+ Euro
gen r2beq150p = .
missing_common ex006_, result(r2beq150p)
replace r2beq150p = .p if inlist(r2proxy,1,2,.d,.m,.r)
replace r2beq150p = ex006_ if inrange(ex006_,0,100)
replace r2beq150p = 0 if ex004_ == 0
label variable r2beq150p "r2beq150p:w2 R prob leave bequest 150k+ euro"

*wave 2 spouse probability leave beqest 150K+ Euro
gen s2beq150p = .
spouse r2beq150p, result(s2beq150p) wave(2)
label variable s2beq150p "s2beq150p:w2 S prob leave bequest 150k+ euro"

***Probability Leave Bequest Any***
*wave 2 respondent probability leave bequest any
gen r2beqany = .
missing_common ex005_, result(r2beqany)
replace r2beqany = .p if inlist(r2proxy,1,2,.d,.m,.r)
replace r2beqany = .s if inrange(ex004_,1,100)
replace r2beqany = ex005_ if inrange(ex005_,0,100)
label variable r2beqany "r2beqany:w2 R prob leave bequest any"

*wave 2 spouse probability leave bequest any
gen s2beqany = .
spouse r2beqany, result(s2beqany) wave(2)
label variable s2beqany "s2beqany:w2 S prob leave bequest any"

***Probability Live to Specific Age***
*wave 2 respondent probability live to specific age
gen r2liv10 = .
missing_common ex009_, result(r2liv10)
replace r2liv10 = .p if inlist(r2proxy,1,2,.d,.m,.r) & r2liv10 == .m
replace r2liv10 = ex009_ if inrange(ex009_,0,100)
label variable r2liv10 "r2liv10:w2 R probability to live 75-120"

*wave 2 spouse probability live to specific age
gen s2liv10 = .
spouse r2liv10, result(s2liv10) wave(2)
label variable s2liv10 "s2liv10:w2 S probability to live 75-120"

***Age Used in Probability Live to Specific Age***
*wave 2 respondent age used in probability live to specific age
gen r2liv10a = .
missing_common ex009age, result(r2liv10a)
replace r2liv10a = .p if inlist(r2proxy,1,2,.d,.m,.r) & r2liv10a == .m
replace r2liv10a = ex009age if inrange(ex009age,75,120)
label variable r2liv10a "r2liv10a:w2 R age used in live 75-120"

*wave 2 spouse age used in probability live to specific age
gen s2liv10a = .
spouse r2liv10a, result(s2liv10a) wave(2)
label variable s2liv10a "s2liv10a:w2 S age used in live 75-120"

***Probability Better Standard of Living in 5 Years***
*wave 2 respondent probability better standard of living in 5 years
gen r2retlivb5 = .
missing_common ex010_, result(r2retlivb5)
replace r2retlivb5 = .p if inlist(r2proxy,1,2,.d,.m,.r) & r2retlivb5 == .m
replace r2retlivb5 = ex010_ if inrange(ex010_,0,100)
label variable r2retlivb5 "r2retlivb5:w2 R prob better std living 5 yrs"

*wave 2 spouse probability better standard of living in 5 years
gen s2retlivb5 = .
spouse r2retlivb5, result(s2retlivb5) wave(2)
label variable s2retlivb5 "s2retlivb5:w2 S prob better std living 5 yrs"

***Probability Worse Standard of Living in 5 Years***
*wave 2 respondent probability worse standard of living in 5 years
gen r2retlivw5 = .
missing_common ex011_, result(r2retlivw5)
replace r2retlivw5 = .p if inlist(r2proxy,1,2,.d,.m,.r) & r2retlivw5 == .m
replace r2retlivw5 = ex011_ if inrange(ex011_,0,100)
label variable r2retlivw5 "r2retlivw5:w2 R prob worse std living 5 yrs"

*wave 2 spouse probability worse standard of living in 5 years
gen s2retlivw5 = .
spouse r2retlivw5, result(s2retlivw5) wave(2)
label variable s2retlivw5 "s2retlivw5:w2 S prob worse std living 5 yrs"

***Probability Working Full Time After 63***
*wave 2 respondent probability working full time after 63
gen r2work63 = .
missing_common ex025_, result(r2work63)
replace r2work63 = .p if inlist(r2proxy,1,2,.d,.m,.r)
replace r2work63 = .a if r2agey >= 61 & !mi(r2agey)
replace r2work63 = .w if inlist(ep005_,-2,-1,1,3,4,5,97)
replace r2work63 = ex025_ if inrange(ex025_,0,100)
label variable r2work63 "r2work63:w2 R prob work ft after 63"

*wave 2 spouse probability working full time after 63
gen s2work63 = .
spouse r2work63, result(s2work63) wave(2)
label variable s2work63 "s2work63:w2 S prob work ft after 63"


***drop wave 2 retire file raw variables
drop `retire_w2_ep'

***drop wave 2 expectation file raw variables
drop `retire_w2_ex'

***drop wave 2 demog file raw variables
drop `retire_w2_cv_r'

******************************************************************************************

	

***merge with wave 4 retire data***
local retire_w4_ep  ep005_ ep049_ ep050_ ep328_ ep329_ ep036_
merge 1:1 mergeid using "$wave_4_ep", keepusing(`retire_w4_ep') nogen

***merge with wave 4 expectations data***
local retire_w4_ex  ex002_ ex009_ ex009age ex025_ 
merge 1:1 mergeid using "$wave_4_ex", keepusing(`retire_w4_ex') nogen

***merge with wave 4 technical variables data***
local retire_w4_techvar mn101_ 
merge 1:1 mergeid using "$wave_4_techvar", keepusing(`retire_w4_techvar') nogen

***merge with wave 4 cv_r data***
local retire_w4_cv_r yrbirth
merge 1:m mergeid using "$wave_4_cv_r", keepusing(`retire_w4_cv_r') nogen
drop if mergeid == "no int w.4"


***Whether retired***
*wave 4 respondent whether retired
gen r4retemp=.
missing_common ep005_,result(r4retemp)
replace r4retemp=1 if ep005_ == 1
replace r4retemp=0 if inlist(ep005_,2,3,4,5,97)
label variable r4retemp "r4retemp:w4 R gives Retired in empstat"
label values r4retemp ret

*wave 4 spouse whether retired
gen s4retemp=.
spouse r4retemp, result(s4retemp) wave(4)
label variable s4retemp "s4retemp:w4 S gives Retired in empstat"
label values s4retemp ret

*wave 4 respondent retire year
gen r4retyr=.
missing_common ep050_ ep329_,result(r4retyr)
replace r4retyr=.n if inlist(ep005_,2,3,4,5,97)
replace r4retyr=ep050_ if ep005_==1 & inrange(ep050_,1900,2100)
replace r4retyr=ep329_ if ep005_==1 & inrange(ep329_,1900,2100) 
replace r4retyr=.i if ep329_ < yrbirth & inrange(ep329_,1900,2100) & inrange(yrbirth,1890,2010)
label variable r4retyr "r4retyr:w4 r Year retired if retired"

*wave 4 spouse retire year
gen s4retyr=.
spouse r4retyr, result(s4retyr) wave(4)
label variable s4retyr "s4retyr:w4 s Year retired if retired"

*wave 4 respondent retire month
gen r4retmon=.
missing_common ep328_,result(r4retmon)
replace r4retmon=.n if inlist(ep005_,2,3,4,5,97)
replace r4retmon=ep328_ if ep005_==1 & inrange(ep328_,0,12) 
replace r4retmon=.s if ep328_==. &  inrange(ep329_, 1900, 2008)  
label variable r4retmon "r4retmon:w4 r Month retired if retired"

*wave 4 spouse retire month
gen s4retmon=.
spouse r4retmon, result(s4retmon) wave(4)
label variable s4retmon "s4retmon:w4 s Month retired if retired"

***early retire***
*wae 4 respondent early retire
gen r4retear=.
missing_common ep036_,result(r4retear)
replace r4retear=1 if ep036_ == 1
replace r4retear=0 if ep036_==5
replace r4retear=.w if ep005_!=. & (ep005_ >2|ep005_==1)
label variable r4retear "r4retear:w4 R look for early retirement"
label values r4retear retear

*wave 4 spouse early retire
gen s4retear=.
spouse r4retear, result(s4retear) wave(4)
label variable s4retear "s4retear:w4 S look for early retirement"
label values s4retear retear

***Probability Receive Inheritance in Next 10 Years***
*wave 4 respondent probability receive inheritance in next 10 yrs
gen r4inher = .
missing_common ex002_, result(r4inher)
replace r4inher = .p if inlist(r4proxy,1,2,.d,.m,.r)
replace r4inher = .s if mn101_ == 1 & r4inher == .m
replace r4inher = ex002_ if inrange(ex002_,0,100)
label variable r4inher "r4inher:w4 R prob inheritance next 10 yr"

*wave 4 spouse probability receive inheritance in next 10 yrs
gen s4inher = .
spouse r4inher, result(s4inher) wave(4)
label variable s4inher "s4inher:w4 S prob inheritance next 10 yr"

***Probability Live to Specific Age***
*wave 4 respondent probability live to specific age
gen r4liv10 = .
missing_common ex009_, result(r4liv10)
replace r4liv10 = .p if inlist(r4proxy,1,2,.d,.m,.r) & r4liv10 == .m
replace r4liv10 = .s if mn101_ == 1 & r4liv10 == .m
replace r4liv10 = ex009_ if inrange(ex009_,0,100)
label variable r4liv10 "r4liv10:w4 R probability to live 75-120"

*wave 4 spouse probability live to specific age
gen s4liv10 = .
spouse r4liv10, result(s4liv10) wave(4)
label variable s4liv10 "s4liv10:w4 S probability to live 75-120"

***Age Used in Probability Live to Specific Age***
*wave 4 respondent age used in probability live to specific age
gen r4liv10a = .
missing_common ex009age, result(r4liv10a)
replace r4liv10a = .p if inlist(r4proxy,1,2,.d,.m,.r) & r4liv10a == .m
replace r4liv10a = .s if mn101_ == 1 & r4liv10a == .m
replace r4liv10a = ex009age if inrange(ex009age,75,120)
label variable r4liv10a "r4liv10a:w4 R age used in live 75-120"

*wave 4 spouse age used in probability live to specific age
gen s4liv10a = .
spouse r4liv10a, result(s4liv10a) wave(4)
label variable s4liv10a "s4liv10a:w4 S age used in live 75-120"

***Probability Working Full Time After 63***
*wave 4 respondent probability working full time after 63
gen r4work63 = .
missing_common ex025_, result(r4work63)
replace r4work63 = .p if inlist(r4proxy,1,2,.d,.m,.r)
replace r4work63 = .a if r4agey >= 61 & !mi(r4agey)
replace r4work63 = .s if mn101_ == 1
replace r4work63 = .w if inlist(ep005_,-2,-1,1,3,4,5,97)
replace r4work63 = ex025_ if inrange(ex025_,0,100)
label variable r4work63 "r4work63:w4 R prob work ft after 63"

*wave 4 spouse probability working full time after 63
gen s4work63 = .
spouse r4work63, result(s4work63) wave(4)
label variable s4work63 "s4work63:w4 S prob work ft after 63"


***drop wave 4 retire file raw variables
drop `retire_w4_ep'

***drop wave 4 expectations file raw variables
drop `retire_w4_ex'

***drop wave 4 technical variables file raw variables
drop `retire_w4_techvar'

***drop wave 4 demog file raw variables
drop `retire_w4_cv_r'

******************************************************************************************

	

***merge with wave 5 retire data***
local retire_w5_ep  ep005_ ep050_ ep328_ ep329_ ep036_ ep002_ ep006_
merge 1:1 mergeid using "$wave_5_ep", keepusing(`retire_w5_ep') nogen

***merge with wave 5 expectations data***
local retire_w5_ex  ex009_ ex009age ex025_ 
merge 1:1 mergeid using "$wave_5_ex", keepusing(`retire_w5_ex') nogen

***merge with wave 5 technical variables data***
local retire_w5_techvar  mn101_ 
merge 1:1 mergeid using "$wave_5_techvar", keepusing(`retire_w5_techvar') nogen

***merge with wave 5 cv_r data***
local retire_w5_cv_r yrbirth
merge 1:m mergeid using "$wave_5_cv_r", keepusing(`retire_w5_cv_r') nogen
drop if mergeid == "no int w.5"


***Whether retired***
*wave 5 respondent whether retired
gen r5retemp=.
missing_common ep005_,result(r5retemp)
replace r5retemp=1 if ep005_ == 1
replace r5retemp=0 if inlist(ep005_,2,3,4,5,97)
label variable r5retemp "r5retemp:w5 R gives Retired in empstat"
label values r5retemp ret

*wave 5 spouse whether retired
gen s5retemp=.
spouse r5retemp, result(s5retemp) wave(5)
label variable s5retemp "s5retemp:w5 S gives Retired in empstat"
label values s5retemp ret

*wave 5 respondent retire year
gen r5retyr=.
missing_common ep050_ ep329_,result(r5retyr)
replace r5retyr=.n if inlist(ep005_,2,3,4,5,97)
replace r5retyr=ep050_ if ep005_==1 & inrange(ep050_,1900,2100)
replace r5retyr=ep329_ if ep005_==1 & inrange(ep329_,1900,2100) 
replace r5retyr=.i if ep329_ < yrbirth & inrange(ep329_,1900,2100) & inrange(yrbirth,1890,2010)
label variable r5retyr "r5retyr:w5 r Year retired if retired"

*wave 5 spouse retire year
gen s5retyr=.
spouse r5retyr, result(s5retyr) wave(5)
label variable s5retyr "s5retyr:w5 s Year retired if retired"

*wave 5 respondent retire month
gen r5retmon=.
missing_common ep328_,result(r5retmon)
replace r5retmon=.n if inlist(ep005_,2,3,4,5,97)
replace r5retmon=ep328_ if ep005_==1 & inrange(ep328_,0,12) 
replace r5retmon=.s if ep328_==. &  inrange(ep329_, 1900, 2008)  
label variable r5retmon "r5retmon:w5 r Month retired if retired"

*wave 5 spouse retire month
gen s5retmon=.
spouse r5retmon, result(s5retmon) wave(5)
label variable s5retmon "s5retmon:w5 s Month retired if retired"

***early retire***
*wave 5 respondent early retire
gen r5retear=.
missing_common ep036_,result(r5retear)
replace r5retear=1 if ep036_ == 1
replace r5retear=0 if ep036_==5
replace r5retear=.w if ep005_!=. & (ep005_ >2|ep005_==1)
label variable r5retear "r5retear:w5 R look for early retirement"
label values r5retear retear

*wave 5 spouse early retire
gen s5retear=.
spouse r5retear, result(s5retear) wave(5)
label variable s5retear "s5retear:w5 S look for early retirement"
label values s5retear retear

***Probability Live to Specific Age***
*wave 5 respondent probability live to specific age
gen r5liv10 = .
missing_common ex009_, result(r5liv10)
replace r5liv10 = .p if inlist(r5proxy,1,2,.d,.m,.r) & r5liv10 == .m
replace r5liv10 = ex009_ if inrange(ex009_,0,100)
label variable r5liv10 "r5liv10:w5 R probability to live 75-120"

*wave 5 spouse probability live to specific age
gen s5liv10 = .
spouse r5liv10, result(s5liv10) wave(5)
label variable s5liv10 "s5liv10:w5 S probability to live 75-120"

***Age Used in Probability Live to Specific Age***
*wave 5 respondent age used in probability live to specific age
gen r5liv10a = .
missing_common ex009age, result(r5liv10a)
replace r5liv10a = .p if inlist(r5proxy,1,2,.d,.m,.r) & r5liv10a == .m
replace r5liv10a = ex009age if inrange(ex009age,75,120)
label variable r5liv10a "r5liv10a:w5 R age used in live 75-120"

*wave 5 spouse age used in probability live to specific age
gen s5liv10a = .
spouse r5liv10a, result(s5liv10a) wave(5)
label variable s5liv10a "s5liv10a:w5 S age used in live 75-120"

***Probability Working Full Time After 63***
*wave 5 respondent probability working full time after 63
gen r5work63 = .
missing_common ex025_, result(r5work63)
replace r5work63 = .p if inlist(r5proxy,1,2,.d,.m,.r)
replace r5work63 = .a if r5agey >= 61 & !mi(r5agey)
replace r5work63 = .w if inlist(ep005_,-2,-1,1,3,4,5,97)
replace r5work63 = ex025_ if inrange(ex025_,0,100)
label variable r5work63 "r5work63:w5 R prob work ft after 63"

*wave 5 spouse probability working full time after 63
gen s5work63 = .
spouse r5work63, result(s5work63) wave(5)
label variable s5work63 "s5work63:w5 S prob work ft after 63"


***drop wave 5 retire file raw variables
drop `retire_w5_ep'

***drop wave 5 expectations file raw variables
drop `retire_w5_ex'

***drop wave 5 technical variables file raw variables
drop `retire_w5_techvar'

***drop wave 5 demog file raw variables
drop `retire_w5_cv_r'

******************************************************************************************

	

***merge with wave 6 retire data***
local retire_w6_ep  ep005_ ep050_ ep328_ ep329_ ep036_ ep002_ ep006_
merge 1:1 mergeid using "$wave_6_ep", keepusing(`retire_w6_ep') nogen

***merge with wave 6 expectations data***
local retire_w6_ex  ex009_ ex009age ex025_ 
merge 1:1 mergeid using "$wave_6_ex", keepusing(`retire_w6_ex') nogen
 
***merge with wave 6 technical variables data***
local retire_w6_techvar  mn101_ 
merge 1:1 mergeid using "$wave_6_techvar", keepusing(`retire_w6_techvar') nogen

***merge with wave 6 cv_r data***
local retire_w6_cv_r yrbirth
merge 1:m mergeid using "$wave_6_cv_r", keepusing(`retire_w6_cv_r') nogen
drop if mergeid == "no int w.6"


***Whether retired***
*wave 6 respondent whether retired
gen r6retemp=.
missing_common ep005_,result(r6retemp)
replace r6retemp=1 if ep005_ == 1
replace r6retemp=0 if inlist(ep005_,2,3,4,5,97)
label variable r6retemp "r6retemp:w6 R gives Retired in empstat"
label values r6retemp ret

*wave 6 spouse whether retired
gen s6retemp=.
spouse r6retemp, result(s6retemp) wave(6)
label variable s6retemp "s6retemp:w6 S gives Retired in empstat"
label values s6retemp ret

*wave 6 respondent retire year 
gen r6retyr=.
missing_common ep050_ ep329_,result(r6retyr)
replace r6retyr=.n if inlist(ep005_,2,3,4,5,97)
replace r6retyr=ep050_ if ep005_==1 & inrange(ep050_,1900,2100)
replace r6retyr=ep329_ if ep005_==1 & inrange(ep329_,1900,2100) 
replace r6retyr=.i if ep329_ < yrbirth & inrange(ep329_,1900,2100) & inrange(yrbirth,1890,2010)
label variable r6retyr "r6retyr:w6 r Year retired if retired"

*wave 6 spouse retire year
gen s6retyr=.
spouse r6retyr, result(s6retyr) wave(6)
label variable s6retyr "s6retyr:w6 s Year retired if retired"

*wave 6 respondent retire month
gen r6retmon=.
missing_common ep328_,result(r6retmon)
replace r6retmon=.n if inlist(ep005_,2,3,4,5,97)
replace r6retmon=ep328_ if ep005_==1 & inrange(ep328_,0,12) 
replace r6retmon=.s if ep328_==. &  inrange(ep329_, 1900, 2008)  
label variable r6retmon "r6retmon:w6 r Month retired if retired"

*wave 6 spouse retire month
gen s6retmon=.
spouse r6retmon, result(s6retmon) wave(6)
label variable s6retmon "s6retmon:w6 s Month retired if retired"

***early retire***
*wave 6 respondent early retire
gen r6retear=.
missing_common ep036_,result(r6retear)
replace r6retear=1 if ep036_ == 1
replace r6retear=0 if ep036_==5
replace r6retear=.w if ep005_!=. & (ep005_ >2|ep005_==1)
label variable r6retear "r6retear:w6 R look for early retirement"
label values r6retear retear

*wave 6 spouse early retire
gen s6retear=.
spouse r6retear, result(s6retear) wave(6)
label variable s6retear "s6retear:w6 S look for early retirement"
label values s6retear retear

***Probability Live to Specific Age***
*wave 6 respondent probability live to specific age
gen r6liv10 = .
missing_common ex009_, result(r6liv10)
replace r6liv10 = .p if inlist(r6proxy,1,2,.d,.m,.r) & r6liv10 == .m
replace r6liv10 = ex009_ if inrange(ex009_,0,100)
label variable r6liv10 "r6liv10:w6 R probability to live 75-120"
 
*wave 6 spouse probability live to specific age
gen s6liv10 = .
spouse r6liv10, result(s6liv10) wave(6)
label variable s6liv10 "s6liv10:w6 S probability to live 75-120"

***Age Used in Probability Live to Specific Age***
*wave 6 respondent age used in probability live to specific age
gen r6liv10a = .
missing_common ex009age, result(r6liv10a)
replace r6liv10a = .p if inlist(r6proxy,1,2,.d,.m,.r) & r6liv10a == .m
replace r6liv10a = ex009age if inrange(ex009age,75,120)
label variable r6liv10a "r6liv10a:w6 R age used in live 75-120"

*wave 6 spouse age used in probability live to specific age
gen s6liv10a = .
spouse r6liv10a, result(s6liv10a) wave(6)
label variable s6liv10a "s6liv10a:w6 S age used in live 75-120"

***Probability Working Full Time After 63***
*wave 6 respondent probability working full time after 63
gen r6work63 = .
missing_common ex025_, result(r6work63)
replace r6work63 = .p if inlist(r6proxy,1,2,.d,.m,.r)
replace r6work63 = .a if r6agey >= 61 & !mi(r6agey)
replace r6work63 = .w if inlist(ep005_,-2,-1,1,3,4,5,97)
replace r6work63 = ex025_ if inrange(ex025_,0,100)
label variable r6work63 "r6work63:w6 R prob work ft after 63"

*wave 6 spouse probability working full time after 63
gen s6work63 = .
spouse r6work63, result(s6work63) wave(6)
label variable s6work63 "s6work63:w6 S prob work ft after 63"


***drop wave 6 retire file raw variables
drop `retire_w6_ep'

***drop wave 6 expectations file raw variables
drop `retire_w6_ex'

***drop wave 6 technical variables file raw variables
drop `retire_w6_techvar'

***drop wave 6 demog file raw variables
drop `retire_w6_cv_r'

******************************************************************************************

	

*whether has public pension
label define pupens ///
	0 "0.No public pension" ///
	1 "1.Has public pension"
	
*whether has private pension
label define pripens ///
	0 "0.No private pension" ///
	1 "1.has private pension"





 
******************************************************************************************

	

*set wave number
local wv = 1

***merge with wave 1 pension data***
local pension_w1_ep  ep071d1 ep071d2 ep071d3 ep071d4 ep071d5 ep071d6 ep071d7 ep071d8 ep071d9 ///
			ep071d10 ep071d11 ep081_1 ep081_2 ep081_3 ep081_4 ep081_5 ep081_6 ep081_7 ep081_8 /// 
			ep081_9 ep081_10 ep081_11 ep089d2 ep092_2 ep213_1 ep213_2 ep213_3 ep213_5 ep213_7 ep213_8 ep213_9
merge 1:1 mergeid using "$wave_1_ep", keepusing(`pension_w1_ep') nogen

***merge with wave 1 cv_r data***
local pension_w1_cv_r yrbirth country
merge 1:m mergeid using "$wave_1_cv_r", keepusing(`pension_w1_cv_r') nogen
drop if mergeid == "no int w.1"


***Receives Public Pensions***
*wave 1 respondent receives public pensions
gen r`wv'pubpen = .
missing_common ep071d1 ep071d2 ep071d3 ep071d5 ep071d7 ep071d8 ep071d9 ep081_1 ep081_2 ep081_3 ep081_5 ep081_7 ep081_8 ep081_9, result(r`wv'pubpen)
replace r`wv'pubpen = 0 if inw`wv' == 1 & !inlist(country,13,14,17) & ((ep071d1==0)|(ep071d2==0)|(ep071d5==0)|(ep071d7==0) | ///
		(ep081_1==5)|(ep081_2==5)|(ep081_5==5)|(ep081_7==5)) 
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 13 & ((ep071d1==0)|(ep071d3==0) | (ep081_1==5)|(ep081_3==5)) 
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 14 & ((ep071d1==0)|(ep071d2==0)|(ep071d5==0)|(ep071d7==0)|(ep071d8==0)|(ep071d9==0) | ///
		(ep081_1==5)|(ep081_2==5)|(ep081_5==5)|(ep081_7==5)|(ep081_8==5)|(ep081_9==5)) 
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 17 & ((ep071d1==0)|(ep071d3==0)|(ep071d5==0)|(ep071d7==0) | ///
		(ep081_1==5)|(ep081_3==5)|(ep081_5==5)|(ep081_7==5)) 
replace r`wv'pubpen = 1 if inw`wv' == 1 & !inlist(country,13,14,17) & ((ep071d1==1)|(ep071d2==1)|(ep071d5==1)|(ep071d7==1) | ///
		(ep081_1==1)|(ep081_2==1)|(ep081_5==1)|(ep081_7==1))
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 13 & ((ep071d1==1)|(ep071d3==1) | (ep081_1==1)|(ep081_3==1)) 
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 14 & ((ep071d1==1)|(ep071d2==1)|(ep071d5==1)|(ep071d7==1)|(ep071d8==1)|(ep071d9==1) | ///
		(ep081_1==1)|(ep081_2==1)|(ep081_5==1)|(ep081_7==1)|(ep081_8==1)|(ep081_9==1)) 
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 17 & ((ep071d1==1)|(ep071d3==1)|(ep071d5==1)|(ep071d7==1) | ///
		(ep081_1==1)|(ep081_3==1)|(ep081_5==1)|(ep081_7==1)) 
label variable r`wv'pubpen "r`wv'pubpen:w`wv' whether r receives public pensions"
label values r`wv'pubpen pupens

*wave 1 spouse receives public pensions
gen s`wv'pubpen = .
spouse r`wv'pubpen, result(s`wv'pubpen) wave(`wv')
label variable s`wv'pubpen "s`wv'pubpen:w`wv' whether s receives public pensions"
label values s`wv'pubpen pupens

***Age when Started to Receive Public Pensions***
*wave 1 respondent age started to receive public pension
gen pubage_1 = ep213_1 - yrbirth if ep071d1 == 1 & inrange(yrbirth,1900,2003) & inrange(ep213_1,1900,2004)
gen pubage_2 = ep213_2 - yrbirth if ep071d2 == 1 & inrange(yrbirth,1900,2003) & inrange(ep213_2,1900,2004) & country != 13
gen pubage_3 = ep213_3 - yrbirth if ep071d3 == 1 & inrange(yrbirth,1900,2003) & inrange(ep213_3,1900,2004) & country == 13
gen pubage_5 = ep213_5 - yrbirth if ep071d5 == 1 & inrange(yrbirth,1900,2003) & inrange(ep213_5,1900,2004) & country != 13
gen pubage_7 = ep213_7 - yrbirth if ep071d7 == 1 & inrange(yrbirth,1900,2003) & inrange(ep213_7,1900,2004) & country != 13
gen pubage_8 = ep213_8 - yrbirth if ep071d8 == 1 & inrange(yrbirth,1900,2003) & inrange(ep213_8,1900,2004) & country == 14
gen pubage_9 = ep213_9 - yrbirth if ep071d9 == 1 & inrange(yrbirth,1900,2003) & inrange(ep213_9,1900,2004) & country == 14

gen r`wv'pubage = min(pubage_1, pubage_2, pubage_3, pubage_5, pubage_7, pubage_8, pubage_9)
replace r`wv'pubage = .m if r`wv'pubage == . & inw`wv' == 1
replace r`wv'pubage = .d if r`wv'pubage == .m & (ep213_1==-1|ep213_2==-1|ep213_3==-1|ep213_5==-1|ep213_7==-1|ep213_8==-1|ep213_9==-1|yrbirth==-1)
replace r`wv'pubage = .r if r`wv'pubage == .m & (ep213_1==-2|ep213_2==-2|ep213_3==-2|ep213_5==-2|ep213_7==-2|ep213_8==-2|ep213_9==-2|yrbirth==-2)
replace r`wv'pubage = .n if r`wv'pubpen == 0 & inw`wv' == 1
replace r`wv'pubage = .i if r`wv'pubpen == 1 & r`wv'pubage < 0
label variable r`wv'pubage "r`wv'pubage:w`wv' age r started to receive public pensions"

*wave 1 spouse age started to receive public pension
gen s`wv'pubage = .
spouse r`wv'pubage, result(s`wv'pubage) wave(`wv')
label variable s`wv'pubage "s`wv'pubage:w`wv' age s started to receive public pensions"

drop pubage_1 pubage_2 pubage_3 pubage_5 pubage_7 pubage_8 pubage_9 

***Currently Receives Any Private Pensions***
*wave 1 respondent receives private pensions
gen r`wv'peninc = .
missing_common ep071d4 ep071d5 ep071d6 ep071d7 ep071d8 ep071d9 ep071d10 ep071d11 /// 
		ep081_4 ep081_5 ep081_6 ep081_7 ep081_8 ep081_9 ep081_10 ep081_11 ep089d2 ep092_2, result(r`wv'peninc)
replace r`wv'peninc = 0 if inw`wv' == 1 & !inlist(country,13,14,17) & ((ep071d8==0)|(ep071d9==0)|(ep071d10==0)|(ep071d11==0) | ///
		(ep081_8==5)|(ep081_9==5)|(ep081_10==5)|(ep081_11==5) | (ep089d2==0)|(ep092_2==5))
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 13 & ((ep071d4==0)|(ep071d5==0)|(ep071d6==0)|(ep071d7==0)|(ep071d8==0) | ///
		(ep081_4==5)|(ep081_5==5)|(ep081_6==5)|(ep081_7==5)|(ep081_8==5) | (ep089d2==0)|(ep092_2==5))
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 14 & ((ep071d10==0)|(ep071d11==0) | ///
		(ep081_10==5)|(ep081_11==5) | (ep089d2==0)|(ep092_2==5))
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 17 & ((ep071d2==0)|(ep071d8==0)|(ep071d9==0)|(ep071d10==0)|(ep071d11==0) | ///
		(ep081_2==5)|(ep081_8==5)|(ep081_9==5)|(ep081_10==5)|(ep081_11==5))
replace r`wv'peninc = 1 if inw`wv' == 1 & !inlist(country,13,14,17) & ((ep071d8==1)|(ep071d9==1)|(ep071d10==1)|(ep071d11==1) | ///
		(ep081_8==1)|(ep081_9==1)|(ep081_10==1)|(ep081_11==1) | (ep089d2==1)|(ep092_2==1)) 
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 13 & ((ep071d4==1)|(ep071d5==1)|(ep071d6==1)|(ep071d7==1)|(ep071d8==1) | ///
		(ep081_4==1)|(ep081_5==1)|(ep081_6==1)|(ep081_7==1)|(ep081_8==1) | (ep089d2==1)|(ep092_2==1))
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 14 & ((ep071d10==1)|(ep071d11==1) | ///
		(ep081_10==1)|(ep081_11==1) | (ep089d2==1)|(ep092_2==1)) 
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 17 & ((ep071d2==1)|(ep071d8==1)|(ep071d9==1)|(ep071d10==1)|(ep071d11==1) | ///
		(ep081_2==1)|(ep081_8==1)|(ep081_9==1)|(ep081_10==1)|(ep081_11==1))
label variable r`wv'peninc "r`wv'peninc:w`wv' whether r receives private pensions"
label values r`wv'peninc pripens

*wave 1 spouse receives private pensions
gen s`wv'peninc = .
spouse r`wv'peninc, result(s`wv'peninc) wave(`wv')
label variable s`wv'peninc "s`wv'peninc:w`wv' whether s receives private pensions"
label values s`wv'peninc pripens


***drop wave 1 retire file raw variables
drop `pension_w1_ep'

***drop wave 1 demog file raw variables
drop `pension_w1_cv_r'


******************************************************************************************

	

local wv = 2
local prewv = 1

***merge with wave 2 retire data***
local pension_w2_ep  ep071d1 ep071d2 ep071d3 ep071d4 ep071d5 ep071d6 ep071d7 ep071d8 ep071d9 ep071d10 ep071d11 ep071d12 ep071d13 ep071d14 ep071d15 ///
			ep081_1 ep081_2 ep081_3 ep081_4 ep081_5 ep081_6 ep081_7 ep081_8 ep081_9 ep081_10 ep081_11 ep081_12 ep081_13 ep081_14 ep081_15 ep081_16 /// 
			ep089d2 ep092_2 ep213_1 ep213_2 ep213_3 ep213_7 ep213_8 ep213_9 ep213_11 ep213_12 ep213_13 ep213_14 ep324d1 ep324d2 ep324d3 ep324d4 ep324d5 ep324d6
merge 1:1 mergeid using "$wave_2_ep", keepusing(`pension_w2_ep') nogen

***merge with wave 2 cv_r data***
local pension_w2_cv_r yrbirth country
merge 1:m mergeid using "$wave_2_cv_r", keepusing(`pension_w2_cv_r') nogen
drop if mergeid == "no int w.2"


***Receives Public Pensions***
*wave 2 respondent receives public pensions
gen r`wv'pubpen = .
missing_common ep071d1 ep071d2 ep071d3 ep071d7 ep071d8 ep071d9 ep071d11 ep071d12 ep071d13 ep071d14 ep081_1 ep081_2 ep081_3 ep081_7 ///
		ep081_8 ep081_9 ep081_11 ep081_12 ep081_13 ep081_14, result(r`wv'pubpen)
replace r`wv'pubpen = 0 if inw`wv' == 1 & !inlist(country,14,17) & (ep071d1==0|ep071d2==0|ep071d3==0|ep071d7==0|ep071d8==0|ep071d9==0 | ///
		ep081_1==5|ep081_2==5|ep081_3==5|ep081_7==5|ep081_8==5|ep081_9==5) 
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 14 & (ep071d1==0|ep071d2==0|ep071d3==0|ep071d7==0|ep071d8==0|ep071d9==0|ep071d11==0| ///
		ep071d12==0|ep071d13==0|ep071d14==0 | ep081_1==5|ep081_2==5|ep081_3==5|ep081_7==5|ep081_8==5|ep081_9==5|ep081_11==5|ep081_12==5|ep081_13==5|ep081_14==5) 
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 17 & (ep071d1==0|ep071d3==0|ep071d7==0|ep071d9==0 | ///
		ep081_1==5|ep081_3==5|ep081_7==5|ep081_9==5) 
replace r`wv'pubpen = 1 if inw`wv' == 1 & !inlist(country,14,17) & (ep071d1==1|ep071d2==1|ep071d3==1|ep071d7==1|ep071d8==1|ep071d9==1 | ///
		ep081_1==1|ep081_2==1|ep081_3==1|ep081_7==1|ep081_8==1|ep081_9==1) 
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 14 & (ep071d1==1|ep071d2==1|ep071d3==1|ep071d7==1|ep071d8==1|ep071d9==1|ep071d11==1| ///
		ep071d12==1|ep071d13==1|ep071d14==1 | ep081_1==1|ep081_2==1|ep081_3==1|ep081_7==1|ep081_8==1|ep081_9==1|ep081_11==1|ep081_12==1|ep081_13==1|ep081_14==1)
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 17 & (ep071d1==1|ep071d3==1|ep071d7==1|ep071d9==1 | ///
		ep081_1==1|ep081_3==1|ep081_7==1|ep081_9==1) 
label variable r`wv'pubpen "r`wv'pubpen:w`wv' whether r receives public pensions"
label values r`wv'pubpen pupens

*wave 2 spouse receives public pensions
gen s`wv'pubpen = .
spouse r`wv'pubpen, result(s`wv'pubpen) wave(`wv')
label variable s`wv'pubpen "s`wv'pubpen:w`wv' whether s receives public pensions"
label values s`wv'pubpen pupens

***Age when Started to Receive Public Pensions***
*wave 2 respondent age started to receive public pension
gen pubage_1 = ep213_1 - yrbirth if ep071d1 == 1 & inrange(yrbirth,1900,2005) & inrange(ep213_1,1900,2005)
gen pubage_2 = ep213_2 - yrbirth if ep071d2 == 1 & inrange(yrbirth,1900,2005) & inrange(ep213_2,1900,2005)
gen pubage_3 = ep213_3 - yrbirth if ep071d3 == 1 & inrange(yrbirth,1900,2005) & inrange(ep213_3,1900,2005)
gen pubage_7 = ep213_7 - yrbirth if ep071d7 == 1 & inrange(yrbirth,1900,2005) & inrange(ep213_7,1900,2005)
gen pubage_8 = ep213_8 - yrbirth if ep071d8 == 1 & inrange(yrbirth,1900,2005) & inrange(ep213_8,1900,2005)
gen pubage_9 = ep213_9 - yrbirth if ep071d9 == 1 & inrange(yrbirth,1900,2005) & inrange(ep213_9,1900,2005)
gen pubage_11 = ep213_11 - yrbirth if (ep071d11==1|ep324d1==1) & inrange(yrbirth,1900,2005) & inrange(ep213_11,1900,2005) & country == 14
gen pubage_12 = ep213_12 - yrbirth if (ep071d12==1|ep324d2==1) & inrange(yrbirth,1900,2005) & inrange(ep213_12,1900,2005) & country == 14
gen pubage_13 = ep213_13 - yrbirth if (ep071d13==1|ep324d3==1) & inrange(yrbirth,1900,2005) & inrange(ep213_13,1900,2005) & country == 14
gen pubage_14 = ep213_14 - yrbirth if (ep071d14==1|ep324d4==1) & inrange(yrbirth,1900,2005) & inrange(ep213_14,1900,2005) & country == 14

gen r`wv'pubage = min(pubage_1, pubage_2, pubage_3, pubage_7, pubage_8, pubage_9, pubage_11, pubage_12, pubage_13, pubage_14)
replace r`wv'pubage = r`prewv'pubage if r`wv'pubage == . & inw`wv' == 1
replace r`wv'pubage = .m if r`wv'pubage == . & inw`wv' == 1
replace r`wv'pubage = .d if r`wv'pubage == .m & (ep213_1==-1|ep213_2==-1|ep213_3==-1|ep213_7==-1|ep213_8==-1|ep213_9==-1|ep213_11==-1| ///
		ep213_12==-1|ep213_13==-1|ep213_14==-1)
replace r`wv'pubage = .r if r`wv'pubage == .m & (ep213_1==-2|ep213_2==-2|ep213_3==-2|ep213_7==-2|ep213_8==-2|ep213_9==-2|ep213_11==-2| ///
		ep213_12==-2|ep213_13==-2|ep213_14==-2)
replace r`wv'pubage = .n if r`wv'pubpen == 0 & inw`wv' == 1
replace r`wv'pubage = .i if r`wv'pubpen == 1 & r`wv'pubage < 0
label variable r`wv'pubage "r`wv'pubage:w`wv' age r started to receive public pensions"

*wave 2 spouse age started to receive public pension
gen s`wv'pubage = .
spouse r`wv'pubage, result(s`wv'pubage) wave(`wv')
label variable s`wv'pubage "s`wv'pubage:w`wv' age s started to receive public pensions"

drop pubage_1 pubage_2 pubage_3 pubage_7 pubage_8 pubage_9 pubage_11 pubage_12 pubage_13 pubage_14

***Currently Receives Any Private Pensions***
*wave 2 respondent receives private pensions
gen r`wv'peninc = .
missing_common ep071d11 ep071d12 ep071d13 ep071d14 ep071d15 ep081_11 ep081_12 ep081_13 ep081_14 ep081_15 ep081_16 ep324d1 ep324d2 ep324d3 ep324d4 ep324d5 ep324d6 ep089d2 ep092_2, result(r`wv'peninc)
replace r`wv'peninc = 0 if inw`wv' == 1 & !inlist(country,14,17) & (ep071d11==0|ep071d12==0|ep071d13==0|ep071d14==0|ep071d15==0 | ///
		ep081_11==5|ep081_12==5|ep081_13==5|ep081_14==5|ep081_15==5|ep081_16==5 | ep324d1==0|ep324d2==0|ep324d3==0|ep324d4==0|ep324d5==0|ep324d6==0 | ///
		ep089d2==0|ep092_2==5)
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 14 & (ep324d5==0|ep324d6==0 | ep081_15==5|ep081_16==5 | ep089d2==0|ep092_2==5)
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 17 & (ep071d2==0|ep071d8==0 | ep324d1==0|ep324d2==0|ep324d3==0|ep324d4==0|ep324d5==0|ep324d6==0 | ///
		ep081_2==5|ep081_8==5 | ep089d2==0|ep092_2==5)
replace r`wv'peninc = 1 if inw`wv' == 1 & !inlist(country,14,17) & (ep071d11==1|ep071d12==1|ep071d13==1|ep071d14==1|ep071d15==1 | ///
		ep081_11==1|ep081_12==1|ep081_13==1|ep081_14==1|ep081_15==1|ep081_16==1 | ep324d1==1|ep324d2==1|ep324d3==1|ep324d4==1|ep324d5==1|ep324d6==1 | ///
		ep089d2==1|ep092_2==1)
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 14 & (ep324d5==1|ep324d6==1 | ep081_15==1|ep081_16==1 | ep089d2==1|ep092_2==1)
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 17 & (ep071d2==1|ep071d8==1 | ep324d1==1|ep324d2==1|ep324d3==1|ep324d4==1|ep324d5==1|ep324d6==1 | ///
		ep081_2==1|ep081_8==1 | ep089d2==1|ep092_2==1)
label variable r`wv'peninc "r`wv'peninc:w`wv' whether r receives private pensions"
label values r`wv'peninc pripens

*wave 2 spouse receives private pensions
gen s`wv'peninc = .
spouse r`wv'peninc, result(s`wv'peninc) wave(`wv')
label variable s`wv'peninc "s`wv'peninc:w`wv' whether s receives private pensions"
label values s`wv'peninc pripens


***drop wave 2 retire file raw variables
drop `pension_w2_ep'

***drop wave 2 demog file raw variables
drop `pension_w2_cv_r'

******************************************************************************************

	

local wv = 4
local prewv = 2

***merge with wave 4 pension data***
local pension_w4_ep  ep071d1 ep071d2 ep071d3 ep071d4 ep071d5 ep071d6 ep071d7 ep071d8 ep071d9 ep071d10 ep071d11 ep071d12 ep071d13 ep071d14 ep071d15 ///
			ep081_1 ep081_2 ep081_3 ep081_4 ep081_5 ep081_6 ep081_7 ep081_8 ep081_9 ep081_10 ep081_11 ep081_12 ep081_13 ep081_14 ep081_15 ep081_16 /// 
			ep089d2 ep092_2 ep213_1 ep213_2 ep213_3 ep213_7 ep213_8 ep213_9 ep213_11 ep213_12 ep213_13 ep213_14 ep324d1 ep324d2 ep324d3 ep324d4 ep324d5 ep324d6
merge 1:1 mergeid using "$wave_4_ep", keepusing(`pension_w4_ep') nogen

***merge with wave 4 cv_r data***
local pension_w4_cv_r yrbirth country
merge 1:m mergeid using "$wave_4_cv_r", keepusing(`pension_w4_cv_r') nogen
drop if mergeid == "no int w.4"


***Receives Public Pensions***
*wave 4 respondent receives public pensions
gen r`wv'pubpen = .
missing_common ep071d1 ep071d2 ep071d3 ep071d7 ep071d8 ep071d9 ep071d11 ep071d12 ep071d13 ep071d14 ep081_1 ep081_2 ep081_3 ep081_7 ///
		ep081_8 ep081_9 ep081_11 ep081_12 ep081_13 ep081_14, result(r`wv'pubpen)
replace r`wv'pubpen = 0 if inw`wv' == 1 & !inlist(country,14,17) & (ep071d1==0|ep071d2==0|ep071d3==0|ep071d7==0|ep071d8==0|ep071d9==0 | ///
		ep081_1==5|ep081_2==5|ep081_3==5|ep081_7==5|ep081_8==5|ep081_9==5) 
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 14 & (ep071d1==0|ep071d2==0|ep071d3==0|ep071d7==0|ep071d8==0|ep071d9==0|ep071d11==0| ///
		ep071d12==0|ep071d13==0|ep071d14==0 | ep081_1==5|ep081_2==5|ep081_3==5|ep081_7==5|ep081_8==5|ep081_9==5|ep081_11==5|ep081_12==5|ep081_13==5|ep081_14==5) 
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 17 & (ep071d1==0|ep071d3==0|ep071d7==0|ep071d9==0 | ///
		ep081_1==5|ep081_3==5|ep081_7==5|ep081_9==5) 
replace r`wv'pubpen = 1 if inw`wv' == 1 & !inlist(country,14,17) & (ep071d1==1|ep071d2==1|ep071d3==1|ep071d7==1|ep071d8==1|ep071d9==1 | ///
		ep081_1==1|ep081_2==1|ep081_3==1|ep081_7==1|ep081_8==1|ep081_9==1)
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 14 & (ep071d1==1|ep071d2==1|ep071d3==1|ep071d7==1|ep071d8==1|ep071d9==1|ep071d11==1| ///
		ep071d12==1|ep071d13==1|ep071d14==1 | ep081_1==1|ep081_2==1|ep081_3==1|ep081_7==1|ep081_8==1|ep081_9==1|ep081_11==1|ep081_12==1|ep081_13==1|ep081_14==1)
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 17 & (ep071d1==1|ep071d3==1|ep071d7==1|ep071d9==1 | ///
		ep081_1==1|ep081_3==1|ep081_7==1|ep081_9==1) 
label variable r`wv'pubpen "r`wv'pubpen:w`wv' whether r receives public pensions"
label values r`wv'pubpen pupens

*wave 4 spouse receives public pensions
gen s`wv'pubpen = .
spouse r`wv'pubpen, result(s`wv'pubpen) wave(`wv')
label variable s`wv'pubpen "s`wv'pubpen:w`wv' whether s receives public pensions"
label values s`wv'pubpen pupens

***Age when Started to Receive Public Pensions***
*wave 4 respondent age started to receive public pension
gen pubage_1 = ep213_1 - yrbirth if ep071d1 == 1 & inrange(yrbirth,1900,2010) & inrange(ep213_1,1900,2010)
gen pubage_2 = ep213_2 - yrbirth if ep071d2 == 1 & inrange(yrbirth,1900,2010) & inrange(ep213_2,1900,2010)
gen pubage_3 = ep213_3 - yrbirth if ep071d3 == 1 & inrange(yrbirth,1900,2010) & inrange(ep213_3,1900,2010)
gen pubage_7 = ep213_7 - yrbirth if ep071d7 == 1 & inrange(yrbirth,1900,2010) & inrange(ep213_7,1900,2010)
gen pubage_8 = ep213_8 - yrbirth if ep071d8 == 1 & inrange(yrbirth,1900,2010) & inrange(ep213_8,1900,2010)
gen pubage_9 = ep213_9 - yrbirth if ep071d9 == 1 & inrange(yrbirth,1900,2010) & inrange(ep213_9,1900,2010)
gen pubage_11 = ep213_11 - yrbirth if (ep071d11==1|ep324d1==1) & inrange(yrbirth,1900,2010) & inrange(ep213_11,1900,2010) & country == 14
gen pubage_12 = ep213_12 - yrbirth if (ep071d12==1|ep324d2==1) & inrange(yrbirth,1900,2010) & inrange(ep213_12,1900,2010) & country == 14
gen pubage_13 = ep213_13 - yrbirth if (ep071d13==1|ep324d3==1) & inrange(yrbirth,1900,2010) & inrange(ep213_13,1900,2010) & country == 14
gen pubage_14 = ep213_14 - yrbirth if (ep071d14==1|ep324d4==1) & inrange(yrbirth,1900,2010) & inrange(ep213_14,1900,2010) & country == 14

gen r`wv'pubage = min(pubage_1, pubage_2, pubage_3, pubage_7, pubage_8, pubage_9, pubage_11, pubage_12, pubage_13, pubage_14)
replace r`wv'pubage = r`prewv'pubage if r`wv'pubage == . & inw`wv' == 1
replace r`wv'pubage = .m if r`wv'pubage == . & inw`wv' == 1
replace r`wv'pubage = .d if r`wv'pubage == .m & (ep213_1==-1|ep213_2==-1|ep213_3==-1|ep213_7==-1|ep213_8==-1|ep213_9==-1|ep213_11==-1|ep213_12==-1|ep213_13==-1|ep213_14==-1)
replace r`wv'pubage = .r if r`wv'pubage == .m & (ep213_1==-2|ep213_2==-2|ep213_3==-2|ep213_7==-2|ep213_8==-2|ep213_9==-2|ep213_11==-2|ep213_12==-2|ep213_13==-2|ep213_14==-2)
replace r`wv'pubage = .n if r`wv'pubpen == 0 & inw`wv' == 1
replace r`wv'pubage = .i if r`wv'pubpen == 1 & r`wv'pubage < 0
label variable r`wv'pubage "r`wv'pubage:w`wv' age r started to receive public pensions"

*wave 4 spouse age started to receive public pension
gen s`wv'pubage = .
spouse r`wv'pubage, result(s`wv'pubage) wave(`wv')
label variable s`wv'pubage "s`wv'pubage:w`wv' age s started to receive public pensions"

drop pubage_1 pubage_2 pubage_3 pubage_7 pubage_8 pubage_9 pubage_11 pubage_12 pubage_13 pubage_14

***Currently Receives Any Private Pensions***
*wave 4 respondent receives private pensions
gen r`wv'peninc = .
missing_common ep071d11 ep071d12 ep071d13 ep071d14 ep071d15 ep081_11 ep081_12 ep081_13 ep081_14 ep081_15 ep081_16 ep324d1 ep324d2 ep324d3 ep324d4 ep324d5 ep324d6 ep089d2 ep092_2, result(r`wv'peninc)
replace r`wv'peninc = 0 if inw`wv' == 1 & !inlist(country,14,17) & (ep071d11==0|ep071d12==0|ep071d13==0|ep071d14==0|ep071d15==0 | ///
		ep081_11==5|ep081_12==5|ep081_13==5|ep081_14==5|ep081_15==5|ep081_16==5 | ep324d1==0|ep324d2==0|ep324d3==0|ep324d4==0|ep324d5==0|ep324d6==0 | ///
		ep089d2==0|ep092_2==5)
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 14 & (ep324d5==0|ep324d6==0 | ep081_15==5|ep081_16==5 | ep089d2==0|ep092_2==5) 
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 17 & (ep071d2==0|ep071d8==0 | ep324d1==0|ep324d2==0|ep324d3==0|ep324d4==0|ep324d5==0|ep324d6==0 | ///
		ep081_2==5|ep081_8==5 | ep089d2==0|ep092_2==5)
replace r`wv'peninc = 1 if inw`wv' == 1 & !inlist(country,14,17) & (ep071d11==1|ep071d12==1|ep071d13==1|ep071d14==1|ep071d15==1 | ///
		ep081_11==1|ep081_12==1|ep081_13==1|ep081_14==1|ep081_15==1|ep081_16==1 | ep324d1==1|ep324d2==1|ep324d3==1|ep324d4==1|ep324d5==1|ep324d6==1 | ///
		ep089d2==1|ep092_2==1)
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 14 & (ep324d5==1|ep324d6==1 | ep081_15==1|ep081_16==1 | ep089d2==1|ep092_2==1)
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 17 & (ep071d2==1|ep071d8==1 | ep324d1==1|ep324d2==1|ep324d3==1|ep324d4==1|ep324d5==1|ep324d6==1 | ///
		ep081_2==1|ep081_8==1 | ep089d2==1|ep092_2==1)
label variable r`wv'peninc "r`wv'peninc:w`wv' whether r receives private pensions"
label values r`wv'peninc pripens

*wave 4 spouse receives private pensions
gen s`wv'peninc = .
spouse r`wv'peninc, result(s`wv'peninc) wave(`wv')
label variable s`wv'peninc "s`wv'peninc:w`wv' whether s receives private pensions"
label values s`wv'peninc pripens


***drop wave 4 pension file raw variables
drop `pension_w4_ep'

***drop wave 4 demog file raw variables
drop `pension_w4_cv_r'

******************************************************************************************

	

local wv = 5
local prewv = 4

***merge with wave 5 pension data***
local pension_w5_ep  ep071d1 ep071d2 ep071d3 ep071d4 ep071d5 ep071d6 ep071d7 ep071d8 ep071d9 ep071d10 ///
			ep081_1 ep081_2 ep081_3 ep081_4 ep081_5 ep081_6 ep081_7 ep081_8 ep081_9 ep081_10 ep081_11 ep081_12 ep081_13 ep081_14 ep081_15 ep081_16 /// 
			ep089d2 ep092_2 ep213_1 ep213_2 ep213_3 ep213_7 ep213_8 ep213_9 ep213_11 ep213_12 ep213_13 ep213_14 ep324d1 ep324d2 ep324d3 ep324d4 ep324d5 ep324d6
merge 1:1 mergeid using "$wave_5_ep", keepusing(`pension_w5_ep') nogen

***merge with wave 5 cv_r data***
local pension_w5_cv_r yrbirth country
merge 1:m mergeid using "$wave_5_cv_r", keepusing(`pension_w5_cv_r') nogen
drop if mergeid == "no int w.5"


***Receives Public Pensions***
*wave 5 respondent receives public pensions
gen r`wv'pubpen = .
missing_common ep071d1 ep071d2 ep071d3 ep071d7 ep071d8 ep071d9 ep081_1 ep081_2 ep081_3 ep081_7 ///
		ep081_8 ep081_9 ep081_11 ep081_12 ep081_13 ep081_14 ep324d1 ep324d2 ep324d3 ep324d4, result(r`wv'pubpen)
replace r`wv'pubpen = 0 if inw`wv' == 1 & !inlist(country,14,17,31) & (ep071d1==0|ep071d2==0|ep071d3==0|ep071d7==0|ep071d8==0|ep071d9==0 | ///
		                       ep081_1==5|ep081_2==5|ep081_3==5|ep081_7==5|ep081_8==5|ep081_9==5) 
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 14 & (ep071d1==0|ep071d2==0|ep071d3==0|ep071d7==0|ep071d8==0|ep071d9==0 | ///
		                       ep324d1==0|ep324d2==0|ep324d3==0|ep324d4==0 | ep081_1==5|ep081_2==5|ep081_3==5|ep081_7==5|ep081_8==5|ep081_9==5|ep081_11==5| ///
		                       ep081_12==5|ep081_13==5|ep081_14==5) 
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 17 & (ep071d1==0|ep071d3==0|ep071d7==0|ep071d9==0 | ///
		                       ep081_1==5|ep081_3==5|ep081_7==5|ep081_9==5) 
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 31 & (ep071d1==0|ep071d3==0|ep071d7==0|ep071d9==0 | ///
		                       ep081_1==5|ep081_3==5|ep081_7==5|ep081_9==5) 
replace r`wv'pubpen = 1 if inw`wv' == 1 & !inlist(country,14,17,31) & (ep071d1==1|ep071d2==1|ep071d3==1|ep071d7==1|ep071d8==1|ep071d9==1 | ///
													ep081_1==1|ep081_2==1|ep081_3==1|ep081_7==1|ep081_8==1|ep081_9==1)
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 14 & (ep071d1==1|ep071d2==1|ep071d3==1|ep071d7==1|ep071d8==1|ep071d9==1 | ///
													 ep324d1==1|ep324d2==1|ep324d3==1|ep324d4==1 | ///
													 ep081_1==1|ep081_2==1|ep081_3==1|ep081_7==1|ep081_8==1|ep081_9==1|ep081_11==1| ep081_12==1|ep081_13==1|ep081_14==1)
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 17 & (ep071d1==1|ep071d3==1|ep071d7==1|ep071d9==1 | ///
													ep081_1==1|ep081_3==1|ep081_7==1|ep081_9==1) 
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 31 & (ep071d1==1|ep071d3==1|ep071d7==1|ep071d9==1 | ///
												  ep081_1==1|ep081_3==1|ep081_7==1|ep081_9==1)
label variable r`wv'pubpen "r`wv'pubpen:w`wv' whether r receives public pensions"
label values r`wv'pubpen pupens

*wave 5 spouse receives public pensions
gen s`wv'pubpen = .
spouse r`wv'pubpen, result(s`wv'pubpen) wave(`wv')
label variable s`wv'pubpen "s`wv'pubpen:w`wv' whether s receives public pensions"
label values s`wv'pubpen pupens

***Age when Started to Receive Public Pensions***
*wave 5 respondent age started to receive public pension
gen pubage_1 = ep213_1 - yrbirth if ep071d1 == 1 & inrange(yrbirth,1900,2013) & inrange(ep213_1,1900,2013)
gen pubage_2 = ep213_2 - yrbirth if ep071d2 == 1 & inrange(yrbirth,1900,2013) & inrange(ep213_2,1900,2013) & country != 31
gen pubage_3 = ep213_3 - yrbirth if ep071d3 == 1 & inrange(yrbirth,1900,2013) & inrange(ep213_3,1900,2013)
gen pubage_7 = ep213_7 - yrbirth if ep071d7 == 1 & inrange(yrbirth,1900,2013) & inrange(ep213_7,1900,2013)
gen pubage_8 = ep213_8 - yrbirth if ep071d8 == 1 & inrange(yrbirth,1900,2013) & inrange(ep213_8,1900,2013) & country != 31
gen pubage_9 = ep213_9 - yrbirth if ep071d9 == 1 & inrange(yrbirth,1900,2013) & inrange(ep213_9,1900,2013)
gen pubage_11 = ep213_11 - yrbirth if ep324d1 == 1 & inrange(yrbirth,1900,2013) & inrange(ep213_11,1900,2013) & country == 14
gen pubage_12 = ep213_12 - yrbirth if ep324d2 == 1 & inrange(yrbirth,1900,2013) & inrange(ep213_12,1900,2013) & country == 14
gen pubage_13 = ep213_13 - yrbirth if ep324d3 == 1 & inrange(yrbirth,1900,2013) & inrange(ep213_13,1900,2013) & country == 14
gen pubage_14 = ep213_14 - yrbirth if ep324d4 == 1 & inrange(yrbirth,1900,2013) & inrange(ep213_14,1900,2013) & country == 14

gen r`wv'pubage = min(pubage_1, pubage_2, pubage_3, pubage_7, pubage_8, pubage_9, pubage_11, pubage_12, pubage_13, pubage_14)
replace r`wv'pubage = r`prewv'pubage if r`wv'pubage == . & inw`wv' == 1
replace r`wv'pubage = .m if r`wv'pubage == . & inw`wv' == 1
replace r`wv'pubage = .d if r`wv'pubage == .m & (ep213_1==-1|ep213_2==-1|ep213_3==-1|ep213_7==-1|ep213_8==-1|ep213_9==-1|ep213_11==-1|ep213_12==-1|ep213_13==-1|ep213_14==-1)
replace r`wv'pubage = .r if r`wv'pubage == .m & (ep213_1==-2|ep213_2==-2|ep213_3==-2|ep213_7==-2|ep213_8==-2|ep213_9==-2|ep213_11==-2|ep213_12==-2|ep213_13==-2|ep213_14==-2)
replace r`wv'pubage = .n if r`wv'pubpen == 0 & inw`wv' == 1
replace r`wv'pubage = .i if r`wv'pubpen == 1 & r`wv'pubage < 0
label variable r`wv'pubage "r`wv'pubage:w`wv' age r started to receive public pensions"

*wave 4 spouse age started to receive public pension
gen s`wv'pubage = .
spouse r`wv'pubage, result(s`wv'pubage) wave(`wv')
label variable s`wv'pubage "s`wv'pubage:w`wv' age s started to receive public pensions"

drop pubage_1 pubage_2 pubage_3 pubage_7 pubage_8 pubage_9 pubage_11 pubage_12 pubage_13 pubage_14

***Currently Receives Any Private Pensions***
*wave 5 respondent receives private pensions
gen r`wv'peninc = .
missing_common ep081_11 ep081_12 ep081_13 ep081_14 ep081_15 ep081_16 ep324d1 ep324d2 ep324d3 ep324d4 ep324d5 ep324d6 ep089d2 ep092_2, result(r`wv'peninc)
replace r`wv'peninc = 0 if inw`wv' == 1 & !inlist(country,14,17,31) & (ep081_11==5|ep081_12==5|ep081_13==5|ep081_14==5|ep081_15==5|ep081_16==5 | ///
		ep324d1==0|ep324d2==0|ep324d3==0|ep324d4==0|ep324d5==0|ep324d6==0 | ep089d2==0|ep092_2==5) 
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 14 & (ep324d5==0|ep324d6==0 | ep081_15==5|ep081_16==5 | ep089d2==0|ep092_2==5) 
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 17 & (ep071d2==0|ep071d8==0 | ep324d1==0|ep324d2==0|ep324d3==0|ep324d4==0|ep324d5==0|ep324d6==0 | ///
		ep081_2==5|ep081_8==5 | ep089d2==0|ep092_2==5)
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 31 & (ep081_2==5|ep081_8==5|ep081_11==5|ep081_12==5|ep081_13==5|ep081_14==5|ep081_15==5|ep081_16==5 | ///
		ep071d2==0|ep071d8==0 | ep324d1==0|ep324d2==0|ep324d3==0|ep324d4==0|ep324d5==0|ep324d6==0 | ep089d2==0|ep092_2==5) 
replace r`wv'peninc = 1 if inw`wv' == 1 & !inlist(country,14,17,31) & (ep081_11==1|ep081_12==1|ep081_13==1|ep081_14==1|ep081_15==1|ep081_16==1 | ///
		ep324d1==1|ep324d2==1|ep324d3==1|ep324d4==1|ep324d5==1|ep324d6==1 | ep089d2==1|ep092_2==1)
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 14 & (ep324d5==1|ep324d6==1 | ep081_15==1|ep081_16==1 | ep089d2==1|ep092_2==1)
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 17 & (ep071d2==1|ep071d8==1 | ep324d1==1|ep324d2==1|ep324d3==1|ep324d4==1|ep324d5==1|ep324d6==1 | ///
		ep081_2==1|ep081_8==1 | ep089d2==1|ep092_2==1)
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 31 & (ep081_2==1|ep081_8==1|ep081_11==1|ep081_12==1|ep081_13==1|ep081_14==1|ep081_15==1|ep081_16==1 | ///
		ep071d2==1|ep071d8==1 | ep324d1==1|ep324d2==1|ep324d3==1|ep324d4==1|ep324d5==1|ep324d6==1 | ep089d2==1|ep092_2==1)
label variable r`wv'peninc "r`wv'peninc:w`wv' whether r receives private pensions"
label values r`wv'peninc pripens

*wave 5 spouse receives private pensions
gen s`wv'peninc = .
spouse r`wv'peninc, result(s`wv'peninc) wave(`wv')
label variable s`wv'peninc "s`wv'peninc:w`wv' whether s receives private pensions"
label values s`wv'peninc pripens


***drop wave 5 pension file raw variables
drop `pension_w5_ep'

***drop wave 5 demog file raw variables
drop `pension_w5_cv_r'

******************************************************************************************

	

local wv = 6
local prewv = 5

***merge with wave 6 pension data***
local pension_w6_ep ep671d1 ep671d2 ep671d3 ep671d9 ep671d10 ep671d11 ep081_1 ep081_2 ep081_3 ep081_9 ep081_10 ep081_11  /// 
			ep213_1 ep213_2 ep213_3 ep213_9 ep213_10 ep213_11 ep681_ ep613_ ep624_ ep089d2 ep092_2
merge 1:1 mergeid using "$wave_6_ep", keepusing(`pension_w6_ep') nogen

***merge with wave 6 cv_r data***
local pension_w6_cv_r yrbirth country
merge 1:m mergeid using "$wave_6_cv_r", keepusing(`pension_w6_cv_r') nogen
drop if mergeid == "no int w.6"
 

***Receives Public Pensions***
*wave 6 respondent receives public pensions
gen r`wv'pubpen = .
missing_common ep671d1 ep671d2 ep671d3 ep671d9 ep671d10 ep671d11 ep081_1 ep081_2 ep081_3 ep081_9 ///
		ep081_10 ep081_11, result(r`wv'pubpen)
replace r`wv'pubpen = 0 if inw`wv' == 1 & !inlist(country,13,29,31) & (ep671d1==0|ep671d2==0|ep671d3==0|ep671d9==0|ep671d10==0|ep671d11==0 | ///
		                       ep081_1==5|ep081_2==5|ep081_3==5|ep081_9==5|ep081_10==5|ep081_11==5)                       
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 13 & (ep671d1==0| ep671d9==0 | ep081_1==5|ep081_9==5)
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 29 & (ep671d1==0|ep671d3==0|ep671d9==0|ep671d11==0 | ///
		                       ep081_1==5|ep081_3==5|ep081_9==5|ep081_11==5)                      
replace r`wv'pubpen = 0 if inw`wv' == 1 & country == 31 & (ep671d1==0|ep671d3==0|ep671d9==0|ep671d11==0 | ///
		                       ep081_1==5|ep081_3==5|ep081_9==5|ep081_11==5)                  
replace r`wv'pubpen = 1 if inw`wv' == 1 & !inlist(country,13,29,31) & (ep671d1==1|ep671d2==1|ep671d3==1|ep671d9==1|ep671d10==1|ep671d11==1 | ///
													ep081_1==1|ep081_2==1|ep081_3==1|ep081_9==1|ep081_10==1|ep081_11==1)
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 13 & (ep671d1==1| ep671d9==1| ep081_1==1|ep081_9==1)
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 29 & (ep671d1==1|ep671d3==1|ep671d9==1|ep671d11==1 | ///
													ep081_1==1|ep081_3==1|ep081_9==1|ep081_11==1) 								
replace r`wv'pubpen = 1 if inw`wv' == 1 & country == 31 & (ep671d1==1|ep671d3==1|ep671d9==1|ep671d11==1 | ///
												  ep081_1==1|ep081_3==1|ep081_9==1|ep081_11==1)
												  
label variable r`wv'pubpen "r`wv'pubpen:w`wv' whether r receives public pensions"
label values r`wv'pubpen pupens

*wave 6 spouse receives public pensions
gen s`wv'pubpen = . 
spouse r`wv'pubpen, result(s`wv'pubpen) wave(`wv')
label variable s`wv'pubpen "s`wv'pubpen:w`wv' whether s receives public pensions"
label values s`wv'pubpen pupens

***Age when Started to Receive Public Pensions***
*wave 6 respondent age started to receive public pension 
gen pubage_1 = ep213_1 - yrbirth if ep671d1 == 1 & inrange(yrbirth,1900,2015) & inrange(ep213_1,1930,2015)
gen pubage_2 = ep213_2 - yrbirth if ep671d2 == 1 & inrange(yrbirth,1900,2015) & inrange(ep213_2,1930,2015) & !inlist(country,13,29,31)
gen pubage_3 = ep213_3 - yrbirth if ep671d3 == 1 & inrange(yrbirth,1900,2015) & inrange(ep213_3,1930,2015) & country != 13 
gen pubage_7 = ep213_9 - yrbirth if ep671d9 == 1 & inrange(yrbirth,1900,2015) & inrange(ep213_9,1930,2015) 
gen pubage_8 = ep213_10 - yrbirth if ep671d10 == 1 & inrange(yrbirth,1900,2015) & inrange(ep213_10,1930,2015) & country != 13 
gen pubage_9 = ep213_11 - yrbirth if ep671d11 == 1 & inrange(yrbirth,1900,2015) & inrange(ep213_11,1930,2015) & !inlist(country,13,29,31)

gen r`wv'pubage = min(pubage_1, pubage_2, pubage_3, pubage_7, pubage_8, pubage_9)
replace r`wv'pubage = r`prewv'pubage if r`wv'pubage == . & inw`wv' == 1
replace r`wv'pubage = .m if r`wv'pubage == . & inw`wv' == 1
replace r`wv'pubage = .d if r`wv'pubage == .m & (ep213_1==-1|ep213_2==-1|ep213_3==-1|ep213_9==-1|ep213_10==-1|ep213_11==-1)
replace r`wv'pubage = .r if r`wv'pubage == .m & (ep213_1==-2|ep213_2==-2|ep213_3==-2|ep213_9==-2|ep213_10==-2|ep213_11==-2)
replace r`wv'pubage = .n if r`wv'pubpen == 0 & inw`wv' == 1
replace r`wv'pubage = .i if r`wv'pubpen == 1 & r`wv'pubage < 0
label variable r`wv'pubage "r`wv'pubage:w`wv' age r started to receive public pensions"

*wave 4 spouse age started to receive public pension
gen s`wv'pubage = .
spouse r`wv'pubage, result(s`wv'pubage) wave(`wv')
label variable s`wv'pubage "s`wv'pubage:w`wv' age s started to receive public pensions"

drop pubage_1 pubage_2 pubage_3 pubage_7 pubage_8 pubage_9 

***Currently Receives Any Private Pensions***
*wave 6 respondent receives private pensions
gen r`wv'peninc = .
missing_common ep681_ ep624_ ep671d2 ep671d10 ep081_2 ep081_10 ep089d2 ep092_2, result(r`wv'peninc)
replace r`wv'peninc = 0 if inw`wv' == 1 & !inlist(country,13,29,31) & (ep681_==5 |ep624_==0 | ep089d2==0|ep092_2==5) 		
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 13 & (ep681_==5 |ep624_==0 | ep089d2==0|ep092_2==5| ep671d2==0 | ep081_2==5)
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 29 & (ep671d2==0 | ep081_2==5) 
replace r`wv'peninc = 0 if inw`wv' == 1 & country == 31 & (ep681_==5 |ep624_==0 | ep089d2==0|ep092_2==5| ep671d2==0 | ep081_2==5|ep671d10==0 | ep081_10==5 )
replace r`wv'peninc = 1 if inw`wv' == 1 & !inlist(country,13,29,31) & (ep681_==1 |ep624_==1 | ep089d2==1|ep092_2==1)
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 13 & (ep681_==1 |ep624_==1 | ep089d2==1|ep092_2==1 | ep671d2==1 | ep081_2==1)
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 29 & (ep671d2==1 | ep081_2==1) 
replace r`wv'peninc = 1 if inw`wv' == 1 & country == 31 & (ep681_==1 |ep624_==1 | ep089d2==1|ep092_2==1| ep671d2==1 | ep081_2==1 |ep671d10==1 | ep081_10==1 )

label variable r`wv'peninc "r`wv'peninc:w`wv' whether r receives private pensions"
label values r`wv'peninc pripens

*wave 6 spouse receives private pensions
gen s`wv'peninc = .
spouse r`wv'peninc, result(s`wv'peninc) wave(`wv')
label variable s`wv'peninc "s`wv'peninc:w`wv' whether s receives private pensions"
label values s`wv'peninc pripens


***drop wave 6 pension file raw variables
drop `pension_w6_ep'

***drop wave 6 demog file raw variables
drop `pension_w6_cv_r'

******************************************************************************************

	

************************************************************************************
************************************************************************************

***Create inflation multiplier variables
gen c2003cpindex =  .
replace c2003cpindex = 87.5 if isocountry ==  40 // 2003 - Austria
replace c2003cpindex = 86.2 if isocountry ==  56 // 2003 - Belgium
replace c2003cpindex = 82.9 if isocountry == 203 // 2003 - Czech Republic
replace c2003cpindex = 87.4 if isocountry == 208 // 2003 - Denmark
replace c2003cpindex = 73.8 if isocountry == 233 // 2003 - Estonia
replace c2003cpindex = 89.3 if isocountry == 250 // 2003 - France
replace c2003cpindex = 89.6 if isocountry == 276 // 2003 - Germany
replace c2003cpindex = 80.1 if isocountry == 300 // 2003 - Greece
replace c2003cpindex = 69.6 if isocountry == 348 // 2003 - Hungary
replace c2003cpindex = 89.0 if isocountry == 372 // 2003 - Ireland
replace c2003cpindex = 87.0 if isocountry == 376 // 2003 - Israel
replace c2003cpindex = 87.3 if isocountry == 380 // 2003 - Italy
replace c2003cpindex = 85.6 if isocountry == 442 // 2003 - Luxembourg
replace c2003cpindex = 90.0 if isocountry == 528 // 2003 - Netherlands
replace c2003cpindex = 82.2 if isocountry == 616 // 2003 - Poland
replace c2003cpindex = 87.6 if isocountry == 620 // 2003 - Portugal
replace c2003cpindex = 81.8 if isocountry == 705 // 2003 - Slovenia
replace c2003cpindex = 83.5 if isocountry == 724 // 2003 - Spain
replace c2003cpindex = 91.9 if isocountry == 752 // 2003 - Sweden
replace c2003cpindex = 93.8 if isocountry == 756 // 2003 - Switzerland

gen c2004cpindex =  .
replace c2004cpindex = 89.3 if isocountry ==  40 // 2004 - Austria
replace c2004cpindex = 88.0 if isocountry ==  56 // 2004 - Belgium
replace c2004cpindex = 85.3 if isocountry == 203 // 2004 - Czech Republic
replace c2004cpindex = 88.4 if isocountry == 208 // 2004 - Denmark
replace c2004cpindex = 76.0 if isocountry == 233 // 2004 - Estonia
replace c2004cpindex = 91.2 if isocountry == 250 // 2004 - France
replace c2004cpindex = 91.0 if isocountry == 276 // 2004 - Germany
replace c2004cpindex = 82.4 if isocountry == 300 // 2004 - Greece
replace c2004cpindex = 74.2 if isocountry == 348 // 2004 - Hungary
replace c2004cpindex = 91.0 if isocountry == 372 // 2004 - Ireland
replace c2004cpindex = 86.7 if isocountry == 376 // 2004 - Israel
replace c2004cpindex = 89.2 if isocountry == 380 // 2004 - Italy
replace c2004cpindex = 87.5 if isocountry == 442 // 2004 - Luxembourg
replace c2004cpindex = 91.1 if isocountry == 528 // 2004 - Netherlands
replace c2004cpindex = 85.0 if isocountry == 616 // 2004 - Poland
replace c2004cpindex = 89.7 if isocountry == 620 // 2004 - Portugal
replace c2004cpindex = 84.7 if isocountry == 705 // 2004 - Slovenia
replace c2004cpindex = 86.1 if isocountry == 724 // 2004 - Spain
replace c2004cpindex = 92.3 if isocountry == 752 // 2004 - Sweden
replace c2004cpindex = 94.6 if isocountry == 756 // 2004 - Switzerland

gen c2005cpindex =  .
replace c2005cpindex = 91.4 if isocountry ==  40 // 2005 - Austria
replace c2005cpindex = 90.4 if isocountry ==  56 // 2005 - Belgium
replace c2005cpindex = 86.9 if isocountry == 203 // 2005 - Czech Republic
replace c2005cpindex = 90.0 if isocountry == 208 // 2005 - Denmark
replace c2005cpindex = 79.1 if isocountry == 233 // 2005 - Estonia
replace c2005cpindex = 92.7 if isocountry == 250 // 2005 - France
replace c2005cpindex = 92.5 if isocountry == 276 // 2005 - Germany
replace c2005cpindex = 85.3 if isocountry == 300 // 2005 - Greece
replace c2005cpindex = 76.9 if isocountry == 348 // 2005 - Hungary
replace c2005cpindex = 93.1 if isocountry == 372 // 2005 - Ireland
replace c2005cpindex = 87.8 if isocountry == 376 // 2005 - Israel
replace c2005cpindex = 91.0 if isocountry == 380 // 2005 - Italy
replace c2005cpindex = 89.7 if isocountry == 442 // 2005 - Luxembourg
replace c2005cpindex = 92.6 if isocountry == 528 // 2005 - Netherlands
replace c2005cpindex = 86.9 if isocountry == 616 // 2005 - Poland
replace c2005cpindex = 91.8 if isocountry == 620 // 2005 - Portugal
replace c2005cpindex = 86.8 if isocountry == 705 // 2005 - Slovenia
replace c2005cpindex = 89.0 if isocountry == 724 // 2005 - Spain
replace c2005cpindex = 92.7 if isocountry == 752 // 2005 - Sweden
replace c2005cpindex = 95.7 if isocountry == 756 // 2005 - Switzerland

gen c2006cpindex =  .
replace c2006cpindex = 92.7 if isocountry ==  40 // 2006 - Austria
replace c2006cpindex = 92.0 if isocountry ==  56 // 2006 - Belgium
replace c2006cpindex = 89.1 if isocountry == 203 // 2006 - Czech Republic
replace c2006cpindex = 91.7 if isocountry == 208 // 2006 - Denmark
replace c2006cpindex = 82.6 if isocountry == 233 // 2006 - Estonia
replace c2006cpindex = 94.3 if isocountry == 250 // 2006 - France
replace c2006cpindex = 93.9 if isocountry == 276 // 2006 - Germany
replace c2006cpindex = 88.0 if isocountry == 300 // 2006 - Greece
replace c2006cpindex = 79.9 if isocountry == 348 // 2006 - Hungary
replace c2006cpindex = 96.8 if isocountry == 372 // 2006 - Ireland
replace c2006cpindex = 89.7 if isocountry == 376 // 2006 - Israel
replace c2006cpindex = 92.9 if isocountry == 380 // 2006 - Italy
replace c2006cpindex = 92.1 if isocountry == 442 // 2006 - Luxembourg
replace c2006cpindex = 93.7 if isocountry == 528 // 2006 - Netherlands
replace c2006cpindex = 88.0 if isocountry == 616 // 2006 - Poland
replace c2006cpindex = 94.6 if isocountry == 620 // 2006 - Portugal
replace c2006cpindex = 88.9 if isocountry == 705 // 2006 - Slovenia
replace c2006cpindex = 92.1 if isocountry == 724 // 2006 - Spain
replace c2006cpindex = 94.0 if isocountry == 752 // 2006 - Sweden
replace c2006cpindex = 96.7 if isocountry == 756 // 2006 - Switzerland

gen c2007cpindex =  .
replace c2007cpindex =  94.7 if isocountry ==  40 // 2007 - Austria
replace c2007cpindex =  93.7 if isocountry ==  56 // 2007 - Belgium
replace c2007cpindex =  91.7 if isocountry == 203 // 2007 - Czech Republic
replace c2007cpindex =  93.3 if isocountry == 208 // 2007 - Denmark
replace c2007cpindex =  88.1 if isocountry == 233 // 2007 - Estonia
replace c2007cpindex =  95.7 if isocountry == 250 // 2007 - France
replace c2007cpindex =  96.1 if isocountry == 276 // 2007 - Germany
replace c2007cpindex =  90.6 if isocountry == 300 // 2007 - Greece
replace c2007cpindex =  86.3 if isocountry == 348 // 2007 - Hungary
replace c2007cpindex = 101.6 if isocountry == 372 // 2007 - Ireland
replace c2007cpindex =  90.1 if isocountry == 376 // 2007 - Israel
replace c2007cpindex =  94.6 if isocountry == 380 // 2007 - Italy
replace c2007cpindex =  94.2 if isocountry == 442 // 2007 - Luxembourg
replace c2007cpindex =  95.2 if isocountry == 528 // 2007 - Netherlands
replace c2007cpindex =  90.2 if isocountry == 616 // 2007 - Poland
replace c2007cpindex =  96.9 if isocountry == 620 // 2007 - Portugal
replace c2007cpindex =  92.1 if isocountry == 705 // 2007 - Slovenia
replace c2007cpindex =  94.7 if isocountry == 724 // 2007 - Spain
replace c2007cpindex =  96.0 if isocountry == 752 // 2007 - Sweden
replace c2007cpindex =  97.4 if isocountry == 756 // 2007 - Switzerland

gen c2008cpindex =  .
replace c2008cpindex =  97.7 if isocountry ==  40 // 2008 - Austria
replace c2008cpindex =  97.9 if isocountry ==  56 // 2008 - Belgium
replace c2008cpindex =  97.5 if isocountry == 203 // 2008 - Czech Republic
replace c2008cpindex =  96.5 if isocountry == 208 // 2008 - Denmark
replace c2008cpindex =  97.2 if isocountry == 233 // 2008 - Estonia
replace c2008cpindex =  98.4 if isocountry == 250 // 2008 - France
replace c2008cpindex =  98.6 if isocountry == 276 // 2008 - Germany
replace c2008cpindex =  94.4 if isocountry == 300 // 2008 - Greece
replace c2008cpindex =  91.5 if isocountry == 348 // 2008 - Hungary
replace c2008cpindex = 105.7 if isocountry == 372 // 2008 - Ireland
replace c2008cpindex =  94.2 if isocountry == 376 // 2008 - Israel
replace c2008cpindex =  97.7 if isocountry == 380 // 2008 - Italy
replace c2008cpindex =  97.4 if isocountry == 442 // 2008 - Luxembourg
replace c2008cpindex =  97.6 if isocountry == 528 // 2008 - Netherlands
replace c2008cpindex =  93.9 if isocountry == 616 // 2008 - Poland
replace c2008cpindex =  99.4 if isocountry == 620 // 2008 - Portugal
replace c2008cpindex =  97.4 if isocountry == 705 // 2008 - Slovenia
replace c2008cpindex =  98.5 if isocountry == 724 // 2008 - Spain
replace c2008cpindex =  99.3 if isocountry == 752 // 2008 - Sweden
replace c2008cpindex =  99.8 if isocountry == 756 // 2008 - Switzerland

gen c2009cpindex =  .
replace c2009cpindex =  98.2 if isocountry ==  40 // 2009 - Austria
replace c2009cpindex =  97.9 if isocountry ==  56 // 2009 - Belgium
replace c2009cpindex =  98.6 if isocountry == 203 // 2009 - Czech Republic
replace c2009cpindex =  97.8 if isocountry == 208 // 2009 - Denmark
replace c2009cpindex =  97.1 if isocountry == 233 // 2009 - Estonia
replace c2009cpindex =  98.5 if isocountry == 250 // 2009 - France
replace c2009cpindex =  98.9 if isocountry == 276 // 2009 - Germany
replace c2009cpindex =  95.5 if isocountry == 300 // 2009 - Greece
replace c2009cpindex =  95.3 if isocountry == 348 // 2009 - Hungary
replace c2009cpindex = 101.0 if isocountry == 372 // 2009 - Ireland
replace c2009cpindex =  97.4 if isocountry == 376 // 2009 - Israel
replace c2009cpindex =  98.5 if isocountry == 380 // 2009 - Italy
replace c2009cpindex =  97.8 if isocountry == 442 // 2009 - Luxembourg
replace c2009cpindex =  98.7 if isocountry == 528 // 2009 - Netherlands
replace c2009cpindex =  97.5 if isocountry == 616 // 2009 - Poland
replace c2009cpindex =  98.6 if isocountry == 620 // 2009 - Portugal
replace c2009cpindex =  98.2 if isocountry == 705 // 2009 - Slovenia
replace c2009cpindex =  98.2 if isocountry == 724 // 2009 - Spain
replace c2009cpindex =  98.9 if isocountry == 752 // 2009 - Sweden
replace c2009cpindex =  99.3 if isocountry == 756 // 2009 - Switzerland

gen c2010cpindex =  .
replace c2010cpindex = 100.0 if isocountry ==  40 // 2010 - Austria
replace c2010cpindex = 100.0 if isocountry ==  56 // 2010 - Belgium
replace c2010cpindex = 100.0 if isocountry == 203 // 2010 - Czech Republic
replace c2010cpindex = 100.0 if isocountry == 208 // 2010 - Denmark
replace c2010cpindex = 100.0 if isocountry == 233 // 2010 - Estonia
replace c2010cpindex = 100.0 if isocountry == 250 // 2010 - France
replace c2010cpindex = 100.0 if isocountry == 276 // 2010 - Germany
replace c2010cpindex = 100.0 if isocountry == 300 // 2010 - Greece
replace c2010cpindex = 100.0 if isocountry == 348 // 2010 - Hungary
replace c2010cpindex = 100.0 if isocountry == 372 // 2010 - Ireland
replace c2010cpindex = 100.0 if isocountry == 376 // 2010 - Israel
replace c2010cpindex = 100.0 if isocountry == 380 // 2010 - Italy
replace c2010cpindex = 105.9 if isocountry == 442 // 2010 - Luxembourg
replace c2010cpindex = 100.0 if isocountry == 528 // 2010 - Netherlands
replace c2010cpindex = 100.0 if isocountry == 616 // 2010 - Poland
replace c2010cpindex = 100.0 if isocountry == 620 // 2010 - Portugal
replace c2010cpindex = 100.0 if isocountry == 705 // 2010 - Slovenia
replace c2010cpindex = 100.0 if isocountry == 724 // 2010 - Spain
replace c2010cpindex = 100.0 if isocountry == 752 // 2010 - Sweden
replace c2010cpindex = 100.0 if isocountry == 756 // 2010 - Switzerland

gen c2011cpindex =  .
replace c2011cpindex = 103.3 if isocountry ==  40 // 2011 - Austria
replace c2011cpindex = 103.5 if isocountry ==  56 // 2011 - Belgium
replace c2011cpindex = 101.9 if isocountry == 203 // 2011 - Czech Republic
replace c2011cpindex = 102.8 if isocountry == 208 // 2011 - Denmark
replace c2011cpindex = 105.0 if isocountry == 233 // 2011 - Estonia
replace c2011cpindex = 102.1 if isocountry == 250 // 2011 - France
replace c2011cpindex = 102.1 if isocountry == 276 // 2011 - Germany
replace c2011cpindex = 103.3 if isocountry == 300 // 2011 - Greece
replace c2011cpindex = 103.9 if isocountry == 348 // 2011 - Hungary
replace c2011cpindex = 102.6 if isocountry == 372 // 2011 - Ireland
replace c2011cpindex = 103.5 if isocountry == 376 // 2011 - Israel
replace c2011cpindex = 102.8 if isocountry == 380 // 2011 - Italy
replace c2011cpindex = 103.4 if isocountry == 442 // 2011 - Luxembourg
replace c2011cpindex = 102.3 if isocountry == 528 // 2011 - Netherlands
replace c2011cpindex = 104.2 if isocountry == 616 // 2011 - Poland
replace c2011cpindex = 103.7 if isocountry == 620 // 2011 - Portugal
replace c2011cpindex = 101.8 if isocountry == 705 // 2011 - Slovenia
replace c2011cpindex = 103.2 if isocountry == 724 // 2011 - Spain
replace c2011cpindex = 103.0 if isocountry == 752 // 2011 - Sweden
replace c2011cpindex = 100.2 if isocountry == 756 // 2011 - Switzerland

gen c2012cpindex =  .
replace c2012cpindex = 105.9 if isocountry ==  40 // 2012 - Austria
replace c2012cpindex = 106.5 if isocountry ==  56 // 2012 - Belgium
replace c2012cpindex = 105.3 if isocountry == 203 // 2012 - Czech Republic
replace c2012cpindex = 105.2 if isocountry == 208 // 2012 - Denmark
replace c2012cpindex = 109.1 if isocountry == 233 // 2012 - Estonia
replace c2012cpindex = 104.1 if isocountry == 250 // 2012 - France
replace c2012cpindex = 104.1 if isocountry == 276 // 2012 - Germany
replace c2012cpindex = 104.9 if isocountry == 300 // 2012 - Greece
replace c2012cpindex = 109.8 if isocountry == 348 // 2012 - Hungary
replace c2012cpindex = 104.3 if isocountry == 372 // 2012 - Ireland
replace c2012cpindex = 105.2 if isocountry == 376 // 2012 - Israel
replace c2012cpindex = 105.9 if isocountry == 380 // 2012 - Italy
replace c2012cpindex = 106.2 if isocountry == 442 // 2012 - Luxembourg
replace c2012cpindex = 104.9 if isocountry == 528 // 2012 - Netherlands
replace c2012cpindex = 108.0 if isocountry == 616 // 2012 - Poland
replace c2012cpindex = 106.5 if isocountry == 620 // 2012 - Portugal
replace c2012cpindex = 104.5 if isocountry == 705 // 2012 - Slovenia
replace c2012cpindex = 105.7 if isocountry == 724 // 2012 - Spain
replace c2012cpindex = 103.9 if isocountry == 752 // 2012 - Sweden
replace c2012cpindex =  99.5 if isocountry == 756 // 2012 - Switzerland

gen c2013cpindex =  .
replace c2013cpindex = 108.0 if isocountry ==  40 // 2013 - Austria
replace c2013cpindex = 107.7 if isocountry ==  56 // 2013 - Belgium
replace c2013cpindex = 106.8 if isocountry == 203 // 2013 - Czech Republic
replace c2013cpindex = 106.1 if isocountry == 208 // 2013 - Denmark
replace c2013cpindex = 112.1 if isocountry == 233 // 2013 - Estonia
replace c2013cpindex = 105.0 if isocountry == 250 // 2013 - France
replace c2013cpindex = 105.7 if isocountry == 276 // 2013 - Germany
replace c2013cpindex = 103.9 if isocountry == 300 // 2013 - Greece
replace c2013cpindex = 111.7 if isocountry == 348 // 2013 - Hungary
replace c2013cpindex = 104.9 if isocountry == 372 // 2013 - Ireland
replace c2013cpindex = 106.9 if isocountry == 376 // 2013 - Israel
replace c2013cpindex = 107.2 if isocountry == 380 // 2013 - Italy
replace c2013cpindex = 108.0 if isocountry == 442 // 2013 - Luxembourg
replace c2013cpindex = 107.5 if isocountry == 528 // 2013 - Netherlands
replace c2013cpindex = 109.0 if isocountry == 616 // 2013 - Poland
replace c2013cpindex = 106.8 if isocountry == 620 // 2013 - Portugal
replace c2013cpindex = 106.3 if isocountry == 705 // 2013 - Slovenia
replace c2013cpindex = 107.2 if isocountry == 724 // 2013 - Spain
replace c2013cpindex = 103.8 if isocountry == 752 // 2013 - Sweden
replace c2013cpindex =  99.3 if isocountry == 756 // 2013 - Switzerland

gen c2014cpindex =  .
replace c2014cpindex = 109.7 if isocountry ==  40 // 2014 - Austria
replace c2014cpindex = 108.0 if isocountry ==  56 // 2014 - Belgium
replace c2014cpindex = 107.2 if isocountry == 203 // 2014 - Czech Republic
replace c2014cpindex = 106.7 if isocountry == 208 // 2014 - Denmark
replace c2014cpindex = 112.0 if isocountry == 233 // 2014 - Estonia
replace c2014cpindex = 105.5 if isocountry == 250 // 2014 - France
replace c2014cpindex = 106.7 if isocountry == 276 // 2014 - Germany
replace c2014cpindex = 102.6 if isocountry == 300 // 2014 - Greece
replace c2014cpindex = 111.5 if isocountry == 348 // 2014 - Hungary
replace c2014cpindex = 105.1 if isocountry == 372 // 2014 - Ireland
replace c2014cpindex = 107.4 if isocountry == 376 // 2014 - Israel
replace c2014cpindex = 107.5 if isocountry == 380 // 2014 - Italy
replace c2014cpindex = 108.7 if isocountry == 442 // 2014 - Luxembourg
replace c2014cpindex = 108.5 if isocountry == 528 // 2014 - Netherlands
replace c2014cpindex = 109.1 if isocountry == 616 // 2014 - Poland
replace c2014cpindex = 106.5 if isocountry == 620 // 2014 - Portugal
replace c2014cpindex = 106.5 if isocountry == 705 // 2014 - Slovenia
replace c2014cpindex = 107.0 if isocountry == 724 // 2014 - Spain
replace c2014cpindex = 103.6 if isocountry == 752 // 2014 - Sweden
replace c2014cpindex =  99.3 if isocountry == 756 // 2014 - Switzerland

gen c2015cpindex =  .
replace c2015cpindex = 110.7 if isocountry ==  40 // 2015 - Austria
replace c2015cpindex = 108.6 if isocountry ==  56 // 2015 - Belgium
replace c2015cpindex = 107.5 if isocountry == 203 // 2015 - Czech Republic
replace c2015cpindex = 107.1 if isocountry == 208 // 2015 - Denmark
replace c2015cpindex = 111.5 if isocountry == 233 // 2015 - Estonia
replace c2015cpindex = 105.6 if isocountry == 250 // 2015 - France
replace c2015cpindex = 106.9 if isocountry == 276 // 2015 - Germany
replace c2015cpindex = 100.8 if isocountry == 300 // 2015 - Greece
replace c2015cpindex = 111.4 if isocountry == 348 // 2015 - Hungary
replace c2015cpindex = 104.7 if isocountry == 372 // 2015 - Ireland
replace c2015cpindex = 106.7 if isocountry == 376 // 2015 - Israel
replace c2015cpindex = 107.5 if isocountry == 380 // 2015 - Italy
replace c2015cpindex = 109.2 if isocountry == 442 // 2015 - Luxembourg
replace c2015cpindex = 109.2 if isocountry == 528 // 2015 - Netherlands
replace c2015cpindex = 108.1 if isocountry == 616 // 2015 - Poland
replace c2015cpindex = 107.0 if isocountry == 620 // 2015 - Portugal
replace c2015cpindex = 106.0 if isocountry == 705 // 2015 - Slovenia
replace c2015cpindex = 106.5 if isocountry == 724 // 2015 - Spain
replace c2015cpindex = 103.6 if isocountry == 752 // 2015 - Sweden
replace c2015cpindex =  98.2 if isocountry == 756 // 2015 - Switzerland


label variable c2003cpindex "2003 consumer price index, 2010=100"
label variable c2004cpindex "2004 consumer price index, 2010=100"
label variable c2005cpindex "2005 consumer price index, 2010=100"
label variable c2006cpindex "2006 consumer price index, 2010=100"
label variable c2007cpindex "2007 consumer price index, 2010=100"
label variable c2008cpindex "2008 consumer price index, 2010=100"
label variable c2009cpindex "2009 consumer price index, 2010=100"
label variable c2010cpindex "2010 consumer price index, 2010=100"
label variable c2011cpindex "2011 consumer price index, 2010=100"
label variable c2012cpindex "2012 consumer price index, 2010=100"
label variable c2013cpindex "2013 consumer price index, 2010=100"
label variable c2014cpindex "2014 consumer price index, 2010=100"
label variable c2015cpindex "2015 consumer price index, 2010=100"

***Update all value labels***
foreach v of var * {
	local vlabel : value label `v'
	if "`vlabel'" != "" {
		label define `vlabel' ///
			.v ".v:SP NR" ///
			.u ".u:Unmar" ///
			.r ".r:Refuse" ///
			.m ".m:Missing" ///
			.d ".d:DK", modify
	}
}

***clean up coupid***
forvalues w = 1/6 {
    rename h`w'coupid w`w'_hh_coup
    egen h`w'coupid = group(hhid`w' w`w'_hh_coup)
    label variable h`w'coupid "Wave_`w' specific couple id number /num"
    
    gen h`w'coupidc =""
    replace h`w'coupidc = string(h`w'coupid, "%05.0f") if !missing(h`w'coupid)
    label variable h`w'coupidc "wave `w' specific couple id number /5-char"
}

***rename country***
rename racountry country
label variable country "SHARE country identifier"
label values country share_countries

*drop respondents who did not respond to at least one wave
drop if inw1!=1 & inw2!=1 & inw4!=1 & inw5!=1 & inw6!=1

***drop temporary indicators of spouse
drop w1spouse w2spouse w3spouse w4spouse w5spouse w6spouse
drop w1_hh_coup w2_hh_coup w3_hh_coup w4_hh_coup w5_hh_coup w6_hh_coup

*drop imputation condition variables
drop ragender_i
drop r?agecat_i
drop h?butype_i
drop h?agecat_i

***drop any temp variables
capture drop __0*

***drop unused raspid variables
drop hawvsamp
drop raspid4
*drop r?shltc s?shltc
*drop r?shltcf s?shltcf


***order variables
order cid ///
	hhid ///
	pn ///
	mergeid ///
	nmergeid ///
	hh?hhid ///
	hh?hhidc ///
	hhid? ///
	country ///
	isocountry ///
	r?lang_s ///
	s?lang_s ///
	implicat ///
	h?coupid ///
	h?coupidc ///
	s?mergeid ///
	s?nmergeid ///
	raspct ///
	raspid1 ///
	raspid2 ///
	raspid3 ///
	inw? ///
	inw?sc ///
	r?iwstat ///
	s?iwstat ///
	hacohort ///
	rasampelig ///
	s?sampelig ///
	r?wtsamp ///
	rapsu1 ///
	rapsu2 ///
	raestrat1 ///
	raestrat2 ///
	r?wthh ///
	ral23wthh ///
	ral34wthh ///
	ral45wthh ///
	ral56wthh ///
	ral16wthh ///
	r?wtresp ///
	ral12wthh ///
	ral12wtrsp ///
	ral23wtrsp ///
	ral34wtrsp ///
	ral45wtrsp ///
	ral56wtrsp ///
	ral16wtrsp ///
	hh?hhresp ///
	h?cpl ///
	r?finr ///
	s?finr ///
	r?hhr ///
	s?hhr ///
	r?famr ///
	s?famr ///
	r?proxy ///
	s?proxy ///
	r?iwm ///
	s?iwm ///
	r?iwy ///
	s?iwy ///
	rabyear ///
	s?byear ///
	rabmonth ///
	s?bmonth ///
	radyear ///
	radmonth ///
	r?agey ///
	s?agey ///
	r?agem ///
	s?agem ///
	ragender ///
	s?gender ///
	raedyrs ///
	s?edyrs ///
	rafedyrs ///
	s?fedyrs ///
	raedisced ///
	s?edisced ///
	rafedisced ///
	s?fedisced ///
	raeducl ///
	s?educl ///
	rafeducl ///
	s?feducl ///
	r?mstat ///
	s?mstat ///
	r?mpart ///
	s?mpart ///
	r?mstath ///
	s?mstath ///
	r?mcurln ///
	s?mcurln ///
	rarelig ///
	s?relig ///
	rabplace ///
	s?bplace ///
	rabcountry ///
	s?bcountry ///
	h?rural ///
	///
    r?shlt ///
	s?shlt ///
	r?shltf ///
	s?shltf ///
	r?shlta ///
	s?shlta ///
	r?shltaf ///
	s?shltaf ///
	r?shltc_s ///
	s?shltc_s ///
	r?hlthlma ///
	s?hlthlma ///
	r?walkra ///
	s?walkra ///
	r?dressa ///
	s?dressa ///
	r?batha ///
	s?batha ///
	r?eata ///
	s?eata ///
	r?beda ///
	s?beda ///
	r?toilta ///
	s?toilta ///
	r?mapa ///
	s?mapa ///
	r?housewka ///
	s?housewka ///
	r?phonea ///
	s?phonea ///
	r?moneya ///
	s?moneya ///
	r?medsa ///
	s?medsa ///
	r?shopa ///
	s?shopa ///
	r?mealsa ///
	s?mealsa ///
	r?walk100a ///
	s?walk100a ///
	r?sita ///
	s?sita ///
	r?chaira ///
	s?chaira ///
	r?climsa ///
	s?climsa ///
	r?clim1a ///
	s?clim1a ///
	r?stoopa ///
	s?stoopa ///
	r?lifta ///
	s?lifta ///
	r?dimea ///
	s?dimea ///
	r?armsa ///
	s?armsa ///
	r?pusha ///
	s?pusha ///
	r?adla ///
	s?adla ///
	r?adlam ///
	s?adlam ///
	r?adlwa ///
	s?adlwa ///
	r?adlwam ///
	s?adlwam ///
	r?iadla ///
	s?iadla ///
	r?iadlam ///
	s?iadlam ///
	r?iadlza ///
	s?iadlza ///
	r?iadlzam ///
	s?iadlzam ///
	r?mobilb ///
	s?mobilb ///
	r?mobilbm ///
	s?mobilbm ///
	r?lgmusa ///
	s?lgmusa ///
	r?lgmusam ///
	s?lgmusam ///
	r?grossa ///
	s?grossa ///
	r?grossam ///
	s?grossam ///
	r?finea ///
	s?finea ///
	r?fineam ///
	s?fineam ///
	r?deprex ///
	s?deprex ///
	r?efforx ///
	s?efforx ///
	r?sleepx ///
	s?sleepx ///
	r?whappx ///
	s?whappx ///
	r?flonex ///
	s?flonex ///
	r?fsadx ///
	s?fsadx ///
	r?goingx ///
	s?goingx ///
	r?enlifx ///
	s?enlifx ///
	r?depres ///
	s?depres ///
	r?effort ///
	s?effort ///
	r?sleepr ///
	s?sleepr ///
	r?whappy ///
	s?whappy ///
	r?flone ///
	s?flone ///
	r?fsad ///
	s?fsad ///
	r?going ///
	s?going ///
	r?enlife ///
	s?enlife ///
	r?cesd ///
	s?cesd ///
	r?cesdm ///
	s?cesdm ///
	r?depress ///
	s?depress ///
	r?pessim ///
	s?pessim ///
	r?suicid ///
	s?suicid ///
	r?guilt ///
	s?guilt ///
	r?sleep ///
	s?sleep ///
	r?intrst ///
	s?intrst ///
	r?irritb ///
	s?irritb ///
	r?appett ///
	s?appett ///
	r?fatig ///
	s?fatig ///
	r?concnt ///
	s?concnt ///
	r?enjoym ///
	s?enjoym ///
	r?tearfl ///
	s?tearfl ///
	r?eurod ///
	s?eurod ///
	r?feurod ///
	s?feurod ///
	r?hibpe ///
	s?hibpe ///
	r?diabe ///
	s?diabe ///
	r?cancre ///
	s?cancre ///
	r?cancrf ///
	s?cancrf ///
	r?lunge ///
	s?lunge ///
	r?hearte ///
	s?hearte ///
	r?heartf ///
	s?heartf ///
	r?stroke ///
	s?stroke ///
	r?strokf ///
	s?strokf ///
	r?arthre ///
	s?arthre ///
	r?psyche ///
	s?psyche ///
	r?asthmae ///
	s?asthmae ///
	r?hchole ///
	s?hchole ///
	r?parkine ///
	s?parkine ///
	r?catrcte ///
	s?catrcte ///
	r?hipfeme ///
	s?hipfeme ///
	r?hipfemf ///
	s?hipfemf ///
	r?alzdem ///
	s?alzdem ///
	r?ulcer ///
	s?ulcer ///
	r?height ///
	s?height ///
	r?weight ///
	s?weight ///
	r?bmi ///
	s?bmi ///
	r?bmicat ///
	s?bmicat ///
	r?vgactx ///
	s?vgactx ///
	r?mdactx ///
	s?mdactx ///
	r?drink ///
	s?drink ///
	r?drinkx ///
	s?drinkx ///
	r?drinkn ///
	s?drinkn ///
	r?drinkn7 ///
	s?drinkn7 ///
	r?smokev ///
	s?smokev ///
	r?smoken ///
	s?smoken ///
	r?smokef ///
	s?smokef ///
	r?shltc ///
	s?shltc ///
	r?shltcf ///
	s?shltcf ///
	///
	r?hosp1y ///
	s?hosp1y ///
	r?hsptim1y ///
	s?hsptim1y ///
	r?hspnit1y ///
	s?hspnit1y ///
	r?nrshom1y ///
	s?nrshom1y ///
	r?nrstim1y ///
	s?nrstim1y ///
	r?nrsnit1y ///
	s?nrsnit1y ///
	r?nhmliv1y ///
	s?nhmliv1y ///
	r?doctor1y ///
	s?doctor1y ///
	r?doctim1y ///
	s?doctim1y ///
	r?homcar1y ///
	s?homcar1y ///
	r?pricar1y ///
	s?pricar1y ///
	r?drugs1w ///
	s?drugs1w ///
	r?dentst1y ///
	s?dentst1y ///
	r?oophos1y ///
	s?oophos1y ///
	r?oopdoc1y ///
	s?oopdoc1y ///
	r?oopdrug1y ///
	s?oopdrug1y ///
	r?oophmcr1y ///
	s?oophmcr1y ///
	r?oopden1y ///
	s?oopden1y ///
	r?oopmd1y ///
	s?oopmd1y ///
	r?higov ///
	s?higov ///
	r?hiothp ///
	s?hiothp ///
	r?hident ///
	s?hident ///
	r?hidrug ///
	s?hidrug ///
	r?hiltc ///
	s?hiltc ///
	h?lifein ///
	///
	r?imrc ///
	s?imrc ///
	r?dlrc ///
	s?dlrc ///
	r?mo ///
	s?mo ///
	r?dy ///
	s?dy ///
	r?yr ///
	s?yr ///
	r?dw ///
	s?dw ///
	r?orient ///
	s?orient ///
	r?verbf ///
	s?verbf ///
	r?numer_s ///
	s?numer_s ///
	r?slfmem ///
	s?slfmem ///
	r?ser7 ///
	s?ser7 ///
	r?tr20 ///
	s?tr20 ///
	r?cogimp ///
	s?cogimp ///
	r?cogothp ///
	s?cogothp ///
	///
	c????cpindex ///
	h?arles ///
	h?afrles ///
	hh?arles ///
	hh?afrles ///
	h?atran ///
	h?aftran ///
	hh?atran ///
	hh?aftran ///
	h?absns ///
	h?afbsns ///
	hh?absns ///
	hh?afbsns ///
	h?airothr ///
	h?afirothr ///	
	hh?airothr ///
	hh?afirothr ///	
	h?achck ///
	h?afchck ///
	hh?achck ///
	hh?afchck ///
	h?abdstk ///
	h?afbdstk ///
	hh?abdstk ///
	hh?afbdstk ///
	h?adebt ///
	h?afdebt ///
	hh?adebt ///
	hh?afdebt ///
	h?ahown ///
	h?ahous ///
	h?afhous ///
	hh?ahous ///
	hh?afhous ///
	h?amort ///
	h?afmort ///
	hh?amort ///
	hh?afmort ///
	h?atoth  ///
	hh?atoth  ///
	h?aftoth  ///
	hh?aftoth  ///
	h?atotb ///
	hh?atotb ///
	h?aftotb ///
	hh?aftotb ///
	h?atotn  ///
	hh?atotn  ///
	h?aftotn  ///
	hh?aftotn  ///
	///
	r?iearn ///
	s?iearn ///
    r?ifearn ///
	s?ifearn ///
	r?itearn ///
	s?itearn ///
    r?itfearn ///
	s?itfearn ///
	r?isemp ///
	s?isemp ///
	r?ifsemp ///
	s?ifsemp ///
	r?itsemp ///
	s?itsemp ///
	r?itfsemp ///
	s?itfsemp ///
	h?irent ///
	hh?irent ///
	h?ifrent ///
	hh?ifrent ///
	h?itrent ///
	hh?itrent ///
	h?itfrent ///
	hh?itfrent ///
	h?itrest ///
	hh?itrest ///
	h?iftrest ///
	hh?iftrest ///
	h?ittrest ///
	hh?ittrest ///
	h?itftrest ///
	hh?itftrest ///
	h?icap  ///
	hh?icap  ///
	h?ifcap    ///
	hh?ifcap    ///
	h?itcap  ///
	hh?itcap  ///
	h?itfcap    ///
	hh?itfcap    ///
	r?ipena  ///
	s?ipena  ///
	r?ifpena  ///
	s?ifpena  ///
	r?itpena  ///
	s?itpena  ///
	r?itfpena  ///
	s?itfpena  ///
	r?issdi ///
	s?issdi ///
	r?ifssdi ///
	s?ifssdi ///
	r?itssdi ///
	s?itssdi ///
	r?itfssdi ///
	s?itfssdi ///
	r?isret ///
	s?isret ///
	r?ifsret ///
	s?ifsret ///
	r?itsret ///
	s?itsret ///
	r?itfsret ///
	s?itfsret ///
	r?ipubpen ///
	s?ipubpen ///
	r?ifpubpen ///
	s?ifpubpen ///
	r?itpubpen ///
	s?itpubpen ///
	r?itfpubpen ///
	s?itfpubpen ///
	r?igxfr  ///
	s?igxfr  ///
	r?ifgxfr  ///
	s?ifgxfr  ///
	r?itgxfr  ///
	s?itgxfr  ///
	r?itfgxfr  ///
	s?itfgxfr  ///
	r?iothr ///
	s?iothr ///
	r?ifothr ///
	s?ifothr ///
	r?itothr ///
	s?itothr ///
	r?itfothr ///
	s?itfothr ///
	hh?iothhh  ///
	hh?ifothhh  ///
	hh?itothhh  ///
	hh?itfothhh  ///
	h?itot ///
	hh?itot ///
	h?iftot ///
	hh?iftot ///
	h?ittot ///
	hh?ittot ///
	h?itftot ///
	hh?itftot ///
	hh?cfoodi1m ///
	hh?cfoodo1m ///
	hh?cfood1m ///
	hh?ctot1m ///
	///
	hh?hhres ///
	h?child ///
	h?fchild ///
	h?grchild ///
	h?fgrchild ///
	r?livbro ///
	s?livbro ///
	r?livsis ///
	s?livsis ///
	r?livsib ///
	s?livsib ///
	r?momliv ///
	s?momliv ///
	r?dadliv ///
	s?dadliv ///
	r?momage ///
	s?momage ///
	r?dadage ///
	s?dadage ///
	r?livpar ///
	s?livpar ///
	h?son ///
	h?dau ///
	h?coresd ///
	h?lvnear ///
	h?kcnt ///
	h?kcntf ///
	r?pcnt ///
	s?pcnt ///
	r?pcntf ///
	s?pcntf ///
	h?raany ///
	h?rccare ///
	h?rrcare ///
	h?rfcare ///
	h?gcaany ///
	h?gccare ///
	h?grcare ///
	h?gfcare ///
	h?gkcare ///
	h?adlcare ///
	h?tcany ///
	h?tcamt ///
	h?tpany ///
	h?tpamt ///
	h?toany ///
	h?toamt ///
	h?tfany ///
	h?tfamt ///
	h?fcany ///
	h?fcamt ///
	h?fpany ///
	h?fpamt ///
	h?foany ///
	h?foamt ///
	h?ffany ///
	h?ffamt ///
	h?frec ///
	h?tgiv ///
	h?ftot ///
	r?socwk ///
	s?socwk ///
	r?socyr ///
	s?socyr ///
	r?socrelg_s ///
	s?socrelg_s ///
	r?relgwk ///
	s?relgwk ///
	r?relgyr ///
	s?relgyr ///
	///
	r?work ///
	s?work ///
	r?work2 ///
	s?work2 ///
	r?slfemp ///
	s?slfemp ///
	r?lbrf_s ///
	s?lbrf_s ///
	r?jhours ///
	s?jhours ///
	r?jhour2 ///
	s?jhour2 ///
	r?jweeks_s ///
	s?jweeks_s ///
	r?jweek2_s ///
	s?jweek2_s ///
	r?wgihr_s ///
	s?wgihr_s ///
	r?wgiwk_s ///
	s?wgiwk_s ///
	r?ffreq ///
	s?ffreq ///
	r?fpayment ///
	s?fpayment ///
	r?fhours ///
	s?fhours ///
	r?jphysa ///
	s?jphysa ///
	r?jpress ///
	s?jpress ///
	r?jisco ///
	s?jisco ///
	r?jnace ///
	s?jnace ///
	r?jcten ///
	s?jcten ///
	r?jlasty ///
	s?jlasty ///
	///
	r?retemp ///
	s?retemp ///
	r?retyr ///
	s?retyr ///
	r?retmon ///
	s?retmon ///
	r?retear ///
	s?retear ///	
	r?work63 ///
	s?work63 ///
	r?retlivb5 ///
	s?retlivb5 ///
	r?retlivw5 ///
	s?retlivw5 ///
	r?liv10 ///
	s?liv10 ///
	r?liv10a ///
	s?liv10a ///
	r?inher ///
	s?inher ///
	r?beq50p ///
	s?beq50p ///
	r?beq150p ///
	s?beq150p ///
	r?beqany ///
	s?beqany ///
	///
	r?pubpen ///
	s?pubpen ///
	r?pubage ///
	s?pubage ///
	r?peninc ///
	s?peninc
	
			
***final sort
sort mergeid

***compress dataset
compress

***remove any notes
notes drop _dta

***save output dataset
save "`output'/H_SHARE_d", replace
